【问题标题】:How to create users with Firebase 9 email and password and save that user's additional data in firebase db collection?如何使用 Firebase 9 电子邮件和密码创建用户并将该用户的其他数据保存在 firebase db 集合中?
【发布时间】:2022-01-15 17:58:30
【问题描述】:

我现在在这个问题上苦苦挣扎了几天。有人可以根据我的代码提供精确的解决方案。请不要在 firebase 文档中引用 mi,因为它非常不清楚。我不熟悉火力基地。在我的代码中,我知道问题出在 handleReg 方法中。目前,我的用户正在创建中。但是,我的 firebase db 集合中没有写入任何数据。我需要为新用户和他想要存储在 firebase db 集合中的附加数据实现相同的 doc id(uid)。请有人提供精确的解决方案。 Firebase 文档没有提供有关如何执行此操作的明确说明,这非常令人沮丧。我还检查了所有堆栈溢出链接。他们没有为这个问题提供解决方案。请帮助

import React, {useState} from "react";
import { View, Button } from "react-native";
import { TextInput } from "react-native-paper";
import { doc, setDoc, collection, addDoc } from "firebase/firestore"; 
import { db } from "../firebase/firebase.authentication";
import { auth } from "../firebase/firebase.authentication";
import { createUserWithEmailAndPassword} from "firebase/auth";

export const RegisterScreen = () => {
  const [email, setEmail] = useState("");
   const [password, setpassword] = useState("");

   const HandleReg = () => {
        createUserWithEmailAndPassword(auth, email, password)
        .then(registredUser => {
            const {uid}= registredUser.user.uid
            const SetData = async ()=>{
                await setDoc(doc(db, "user", uid),{
                    name:"test"
                })  
            }              
        })
    
    }
    return (
        <>
        <View>
        <TextInput value={email}
        onChangeText={(text)=> setEmail(text)}
        />
        <TextInput
        value={password}
        onChangeText={(text)=> setpassword(text)}
        />
        
        <Button title="set" onPress={HandleReg}/>
        </View>
</>

    ); 
} 

还有我的 Firebase js:

import {initializeApp} from "firebase/app"
import { getAuth} from "firebase/auth";
import {getFirestore } from "firebase/firestore"; 

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

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

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore firebase-authentication


    【解决方案1】:

    什么时候调用SetData 函数?尝试重构函数,如下所示:

    const HandleReg = async () => {
      const { user } = await createUserWithEmailAndPassword(auth, email, password)
      await setDoc(doc(db, "user", user.uid), { name:"test" }) 
      console.log('Document Added') 
    }         
    

    【讨论】:

    • 是的,问题出在 set 函数上。我在调用它时遇到了问题。谢谢你帮助我
    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 2016-11-27
    • 2017-11-25
    • 2020-10-08
    • 2016-11-18
    • 2019-06-18
    • 2019-06-02
    • 2019-07-10
    相关资源
    最近更新 更多