首页 > 解决方案 > 注册后数据未存储在 Firestore 中,但身份验证正在运行

问题描述

谁能说出为什么信息没有存储在 Firestore 中,凭据在注册后反映在身份验证中。我希望用于注册的用户名、电子邮件和密码等相同的详细信息也同时存储在 Firestore 中,但我发送的详细信息并未反映在 Firestore 中。我正在从 TextInput 输入数据并将它们设置为状态。此外,我的 firebase 处于测试模式,因此在我创建的 firestore 中允许读写。我已正确导入索引文件中的设置和配置,并且身份验证也可以正常工作

import {
  View,
  Text,
  Button,
  StyleSheet,
  TouchableOpacity,
  SafeAreaView,
  KeyboardAvoidingView,
} from 'react-native';
import CustomTextInput from '../components/CustomTextInput';
import * as firebase from 'firebase';



const SignUp = (props) => {
  const [firstName, setFirstName] = useState('');
  const [secondName, setSecondName] = useState('');
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  const registerUserNoThunk = async () => {
    const user = {firstName, secondName, email, password};

    try {
      const result = await firebase
        .auth()
        .createUserWithEmailAndPassword(user.email, user.password);
      // const data = await result.json();
      console.log(result);
      console.log(firebase.auth().currentUser.uid, 'userid');
      const usersdata = await firebase
        .firestore()
        .collection('users')
        .doc(firebase.auth().currentUser.uid,)
        .set({firstName, secondName, email});
      console.log(usersdata, 'usersdata');
    } catch (error) {
      console.log(error);
    }
  };

  return (
    <>
      <SafeAreaView style={{flex: 1}}>
        <KeyboardAvoidingView
          style={{flex: 1}}
          behavior="height"
          keyboardVerticalOffset="10">
          <View style={styles.mainview}>
            <View style={styles.card}>
              <CustomTextInput
                label="First Name"
                value={firstName}
                onChangeText={(text) => {
                  setFirstName(text);
                }}
              />
              <CustomTextInput
                label="Second Name"
                value={secondName}
                onChangeText={(text) => {
                  setSecondName(text);
                }}
              />
              <CustomTextInput
                label="Email"
                value={email}
                onChangeText={(text) => {
                  setEmail(text);
                }}
              />
              <CustomTextInput
                label="Password"
                value={password}
                onChangeText={(text) => {
                  setPassword(text);
                }}
                secureTextEntry
              />
              {/* <View>
                <Button title="Sign Up" onPress={registerUser} />
              </View> */}
            </View>
            <TouchableOpacity
              onPress={() => {
                props.navigation.navigate('ScreenOne');
              }}>
              <View>
                <Text>Already a member? Login</Text>
              </View>
            </TouchableOpacity>
            <View>
              <Button title="Sign Up" onPress={registerUserNoThunk} />
            </View>
          </View>
        </KeyboardAvoidingView>
      </SafeAreaView>
    </>
  );
};

Custom Text Input File

import React from 'react';
import {View, Text, TextInput, StyleSheet} from 'react-native';

const CustomTextInput = (props) => {
  return (
    <View style={styles.inputcontainer}>
      <Text>{props.label}</Text>
      <TextInput {...props} style={styles.inputstyle} />
    </View>
  );
};

const styles = StyleSheet.create({
  inputstyle: {
    borderBottomWidth: 2,
    borderBottomColor: 'grey',
    height: 30,
    padding: 0,
  },
  inputcontainer: {marginVertical: '5%', width: '100%'},
});

export default CustomTextInput;

标签: javascriptreactjsfirebasereact-nativegoogle-cloud-firestore

解决方案


推荐阅读