【问题标题】:Cannot able to upload user Profile react native firebase无法上传用户个人资料反应原生火力基地
【发布时间】:2022-01-03 15:07:15
【问题描述】:

我想更新用户配置文件,所以我创建了 Profile js 文件,但无法检索用户数据,因为我没有将用户数据存储在数据库中 那么如何更新活动用户帐户数据名称、电子邮件、照片、密码。所以下面是我的注册用户帐户代码。我是否需要将用户数据存储在数据库中?

注册屏幕

    const RegisterScreen = ({ navigation }) => {
          const [name, setName] = useState("")
            const [email, setEmail] = useState("")
            const [password, setPassword] = useState("")
            const [image, setImage] = useState(null);
              
            const pickImage = async () => {
        
               // No permissions request is necessary for launching the image library
                let result = await   ImagePicker.launchImageLibraryAsync({
                 
                  mediaTypes: ImagePicker.MediaTypeOptions.All,
                  allowsEditing: true,
                  aspect: [4, 3],
                  quality: 1,
                });
          
              
                console.log(result);
            
                if (!result.cancelled) {
                  setImage(result.uri);
                  
                  
                }
              
              };
        
     
            useLayoutEffect(()=>{
        
                navigation.setOptions({
                    headerBackTitle:"Back to Login"
        
                });
        
            },[navigation]);
        
            const register = () => {
        
                
                auth.createUserWithEmailAndPassword(email, password)
               
        
                .then((authUser) => {

   db.collection('users').set(doc(user.uid).set({
                displayName: name,
                photoURL: image,
                password: password,
                email: email,
    
            }));
                    authUser.user.updateProfile({
                        displayName: name,
                        photoURL: image
                    })
        
                   
                })
                .catch(error => alert(error.message));
               
            }
        
            return (
        
                
                <KeyboardAvoidingView behavior='padding' style={styles.container}>
        
                    <ScrollView>
                
                    <StatusBar style="light" />
                    <Text h3 style={{ marginBottom: 50 }}>
                        Create a Signal account
                    </Text>
                    <View style={styles.inputContainer}>
                        <Input
                            placeholder="Full Name"
                            autoFocus
                            type="text"
                            value={name}
                            onChangeText={(text) => setName(text)}
                        />
                        <Input
                            placeholder="Email"
                            type="email"
                            value={email}
                            onChangeText={(text) => setEmail(text)}
                        />
                        <Input
                            placeholder="Password"
                            type="password"
                            secureTextEntry
                            value={password}
                            onChangeText={(text) => setPassword(text)}
                        />
        
                      
                    </View>
                    
                    <Button buttonStyle={{ backgroundColor: '#49C958ed',}} 
                        containerStyle={styles.button}
                
                        raised title="Upload Image"   onPress={pickImage} />
                    
                    <Button  buttonStyle={{ backgroundColor: '#49C958ed',}} 
                        containerStyle={styles.button}
                        raised title="Register" 
                        onPress={register} 
                    />
        
        </ScrollView>    
                </KeyboardAvoidingView>
            )
        }

【问题讨论】:

    标签: reactjs firebase react-native firebase-realtime-database firebase-authentication


    【解决方案1】:

    您提到的四个属性(姓名、电子邮件、照片、密码)都可以存储在 Firebase 身份验证用户配置文件中。对于姓名和照片,您可以像以前一样使用updateProfile,而对于电子邮件和密码,您将使用signInWithEmailAndPassword,如图所示here

    如果您想存储其他属性,或者想以 Firebase 身份验证不允许的方式使用这些属性,您确实需要将这些属性存储在数据库中,例如 Firestore 的实时数据库或 Cloud Firestore。

    【讨论】:

    • 现在我将我的用户信息存储到了 Firestore,但我担心这可能不会更新信息,因为 Firestore 中的 Uid 与身份验证中存在的用户 ID 不同。基本上我需要让他们的用户的个人资料页面可以更改其数据名称图像等
    • “firestore Uid 中的用户 ID 与身份验证中的用户 ID 不同”这听起来像是您想要修复的问题。考虑调用set(doc(collectionRef, user.uid), {...}),以便将用户的 UID 用作 Firestore 中的文档 ID,而不是调用 addDoc()(它会生成自己的文档 ID)。
    • 得到错误文档未定义
    • db.collection('users').set(doc(user.uid).set({ displayName: name, photoURL: image, password: password, email: email, }));
    • 我也在代码中添加了这个,请问如何使 auth id 和 firestore Id 相同,因为我需要更改信息并更新它
    猜你喜欢
    • 2020-08-26
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2020-06-26
    • 2016-12-21
    • 2016-09-17
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多