我可以帮你翻译这句话:如何在React Native中创建特定集合中的Firestore文档?

huangapple go评论65阅读模式
英文:

How can I create a Firestore document in a specific collection in React Native?

问题

Here is the translated code part you requested:

import { auth, db } from '../firebaseConfig';

const registerUser = async () => {
    if (password !== confirmPassword) {
      alert("Passwords don't match.");
      return;
    }
  
    try {
      const userCredential = await createUserWithEmailAndPassword(auth, email, password);
      const { user } = userCredential;
      const userProfile = {
        uid: user.uid,
        firstName,
        lastName,
        email,
        team,
        completedCourses: [],
      };
  
      try {
        console.log(team);
        console.log(user.uid);
        console.log(userProfile);
        await db.collection(team).doc(user.uid).set(userProfile); // Creating the document.

      } catch (error) {
        console.error("Error creating user document: ", error);
      }
      alert('User registered successfully');
    } catch (error) {
      alert(error.message);
    }
  };

And here's the translated Firebase config:

import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { getAuth, createUserWithEmailAndPassword } from 'firebase/auth';

const firebaseConfig = {
    apiKey: "...",
    authDomain: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "...",
    appId: "...",
    measurementId: "..."
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const auth = getAuth(app);
export { auth, db };

I've made the necessary translation and also fixed a typo in the completedCourses field in the userProfile object.

英文:

I want to create a firestore document in a spesific collection from react native. I want to save some user info, and make 1 collection for each team. So when the user sign up, he gives up what team he belongs to, and i want to save the document in that team-collection.

import { auth, db, firestore } from '../firebaseConfig';

const registerUser = async () => {
    if (password !== confirmPassword) {
      alert("Passwords don't match.");
      return;
    }
  
    try {
      const userCredential = await createUserWithEmailAndPassword(auth, email, password);
      const { user } = userCredential;
      const userProfile = {
        uid: user.uid,
        firstName,
        lastName,
        email,
        team,
        fullførteKurs: [],
      };
  
      try {
        console.log(team);
        console.log(user.uid);
        console.log(userProfile);
        await db.collection(team).doc(user.uid).set(userProfile); //Creating the document.

      } catch (error) {
        console.error("Error creating user document: ", error);
      }
      alert('User registered successfully');
    } catch (error) {
      alert(error.message);
    }
  };

Here is the firebase config, I belive its set up correct. At least the login works and i can confirm i can read data from firestore.

import { initializeApp } from 'firebase/app';
import { getFirestore, collection, getDocs } from 'firebase/firestore';
import { getAuth } from 'firebase/auth';

const firebaseConfig = {
    apiKey: "...",
    authDomain: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "...",
    appId: "...",
    measurementId: "..."
};

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const auth = getAuth(app);
export { auth, db, collection, getDocs };

The console error message that i am getting is this:

Error creating user document: , [TypeError: undefined is not a function]
at screens\RegisterScreen.js:null in registerUser
at node_modules\@babel\runtime\helpers\asyncToGenerator.js:null in asyncGeneratorStep
at node_modules\@babel\runtime\helpers\asyncToGenerator.js:null in _next
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:null in _allocateCallback$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:null in _callTimer
- ... 8 more stack frames from framework internals  

答案1

得分: 0

从你导入和调用 createUserWithEmailAndPassword 的方式看,似乎你正在使用 Firebase SDK 版本 9 及更高的较新、模块化的语法。

但在 db.collection(team).doc(user.uid).set(userProfile) 中,你试图使用旧版本的 SDK(8 及之前)中的名称空间语法调用 Firestore。你不能混合使用这样不同版本的语法。

该代码的模块化等效方式是:

setDoc(doc(db, team, user.uid), userProfile)

不要忘记导入 setDocdoc

另请参阅:

  • Firebase 文档中关于 设置文档 的 v9/模块化语法的代码示例
  • 升级指南 ,针对 JavaScript SDK 的 v9 版本
英文:

From the way you import and call createUserWithEmailAndPassword, it looks like you're using the newer, modular syntax of Firebase SDK versions 9 and later.

But then in db.collection(team).doc(user.uid).set(userProfile) you try to call Firestore in the older, namespaced syntax of SDK versions 8 and before. You can't mix-and-match syntax versions like that.

The modular equivalent of that code is:

setDoc(doc(db, team, user.uid), userProfile)

Don't forget to import setDoc and doc.

Also see:

  • The code sample for the v9/modular syntax in the Firebase documentation on setting a document
  • The upgrade guide for the v9 version of the JavaScript SDKs

huangapple
  • 本文由 发表于 2023年5月22日 19:43:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定