简体   繁体   English

如何使用 Firebase 9 email 和密码创建用户并将该用户的附加数据保存在 firebase 数据库集合中?

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

I am struggling now with this question for couple of days.我现在在这个问题上挣扎了几天。 Can somebody provide precise solution based on my code.有人可以根据我的代码提供精确的解决方案。 Please do not refer mi on firebase documentation because it is very unclear.请不要参考 firebase 文档上的 mi,因为它非常不清楚。 I am not familiar with firebase.我不熟悉firebase。 In my code I know the problem is somewhere in handleReg method.在我的代码中,我知道问题出在 handleReg 方法中。 Curentlly, my user is being created.目前,我的用户正在创建中。 However, no data is writen in my firebase db collection.但是,我的 firebase 数据库集合中没有写入任何数据。 I need to achieve to have same doc id(uid) for the new users and his aditional data that i want to store in firebase db collection.我需要为新用户和他想要存储在 firebase 数据库集合中的附加数据实现相同的 doc id(uid)。 Please somebody provide precise solution.请有人提供精确的解决方案。 It is very frustrating that Firebase documentation does not provide a clear explanation on how to do it.令人非常沮丧的是 Firebase 文档没有提供有关如何执行此操作的明确说明。 Also I check all stack overflow links.我还检查了所有堆栈溢出链接。 They are not offering solution to this question.他们没有为这个问题提供解决方案。 Pleas Help请帮助

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>
</>

    ); 
} 

And My Firebase js:还有我的 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);

When it the SetData function being called?何时调用SetData function? Try refactoring the function as shown below:尝试重构 function,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM