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

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

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

问题

Here is the translated code part you requested:

  1. import { auth, db } from '../firebaseConfig';
  2. const registerUser = async () => {
  3. if (password !== confirmPassword) {
  4. alert("Passwords don't match.");
  5. return;
  6. }
  7. try {
  8. const userCredential = await createUserWithEmailAndPassword(auth, email, password);
  9. const { user } = userCredential;
  10. const userProfile = {
  11. uid: user.uid,
  12. firstName,
  13. lastName,
  14. email,
  15. team,
  16. completedCourses: [],
  17. };
  18. try {
  19. console.log(team);
  20. console.log(user.uid);
  21. console.log(userProfile);
  22. await db.collection(team).doc(user.uid).set(userProfile); // Creating the document.
  23. } catch (error) {
  24. console.error("Error creating user document: ", error);
  25. }
  26. alert('User registered successfully');
  27. } catch (error) {
  28. alert(error.message);
  29. }
  30. };

And here's the translated Firebase config:

  1. import { initializeApp } from 'firebase/app';
  2. import { getFirestore } from 'firebase/firestore';
  3. import { getAuth, createUserWithEmailAndPassword } from 'firebase/auth';
  4. const firebaseConfig = {
  5. apiKey: "...",
  6. authDomain: "...",
  7. projectId: "...",
  8. storageBucket: "...",
  9. messagingSenderId: "...",
  10. appId: "...",
  11. measurementId: "..."
  12. };
  13. const app = initializeApp(firebaseConfig);
  14. const db = getFirestore(app);
  15. const auth = getAuth(app);
  16. 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.

  1. import { auth, db, firestore } from '../firebaseConfig';
  2. const registerUser = async () => {
  3. if (password !== confirmPassword) {
  4. alert("Passwords don't match.");
  5. return;
  6. }
  7. try {
  8. const userCredential = await createUserWithEmailAndPassword(auth, email, password);
  9. const { user } = userCredential;
  10. const userProfile = {
  11. uid: user.uid,
  12. firstName,
  13. lastName,
  14. email,
  15. team,
  16. fullførteKurs: [],
  17. };
  18. try {
  19. console.log(team);
  20. console.log(user.uid);
  21. console.log(userProfile);
  22. await db.collection(team).doc(user.uid).set(userProfile); //Creating the document.
  23. } catch (error) {
  24. console.error("Error creating user document: ", error);
  25. }
  26. alert('User registered successfully');
  27. } catch (error) {
  28. alert(error.message);
  29. }
  30. };

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.

  1. import { initializeApp } from 'firebase/app';
  2. import { getFirestore, collection, getDocs } from 'firebase/firestore';
  3. import { getAuth } from 'firebase/auth';
  4. const firebaseConfig = {
  5. apiKey: "...",
  6. authDomain: "...",
  7. projectId: "...",
  8. storageBucket: "...",
  9. messagingSenderId: "...",
  10. appId: "...",
  11. measurementId: "..."
  12. };
  13. const app = initializeApp(firebaseConfig);
  14. const db = getFirestore(app);
  15. const auth = getAuth(app);
  16. export { auth, db, collection, getDocs };

The console error message that i am getting is this:

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

答案1

得分: 0

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

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

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

  1. 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:

  1. 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:

确定