如何使用 Firebase 9 电子邮件和密码创建用户并将该用户的其他数据保存在 firebase 数据库集合中?

How to create users with Firebase 9 email and password and save that user's additional data in firebase db collection?

我这几天一直在为这个问题苦苦挣扎。有人可以根据我的代码提供精确的解决方案吗?请不要在 firebase 文档中提及 mi,因为它非常不清楚。我不熟悉火力地堡。在我的代码中,我知道问题出在 handleReg 方法的某处。 Curentlly,正在创建我的用户。但是,我的 firebase 数据库集合中没有写入任何数据。我需要为新用户和我想存储在 firebase db 集合中的其他数据实现相同的文档 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);

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

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