繁体   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?

我现在在这个问题上挣扎了几天。 有人可以根据我的代码提供精确的解决方案。 请不要参考 firebase 文档上的 mi,因为它非常不清楚。 我不熟悉firebase。 在我的代码中,我知道问题出在 handleReg 方法中。 目前,我的用户正在创建中。 但是,我的 firebase 数据库集合中没有写入任何数据。 我需要为新用户和他想要存储在 firebase 数据库集合中的附加数据实现相同的 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);

何时调用SetData function? 尝试重构 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