【问题标题】:First argument "email" must be a valid string第一个参数“email”必须是一个有效的字符串
【发布时间】:2019-10-31 10:53:21
【问题描述】:

我最近在练习 React Native 并开发了一个附加到 Firebase 的注册表单。

但是每当我尝试点击按钮时,它都会报错:

createUserWithEmailAndPassword 第一个参数失败,email 必须是有效字符串。

这是我的代码:

import React, {Component} from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {Button,Input} from 'react-native-elements';
import firebase from './screens/firebase';

console.log(firebase.name);
console.log(firebase.database());

class App extends React.Component
{

constructor(props)
{
  super(props);
  this.state = {
    name:'',
    email : '',
    Password : '',
    tc:'',
    phone:'',
    };
}

handleRegisterUser = () => {
    const {name,email,Password,tc,phone} =this.setState;
    firebase.auth().createUserWithEmailAndPassword(email,Password)
    .then((user) => {
        const fbRootRefFS = firebase.firestore();
        const userID = user.uid;
        const userRef = fbRootRefFS.collection('users')
        .doc(userID);
        userRef.set({
            name,
            email,
            Password,
            tc,
            phone,
        });
    })
    }
  render(){
    return(
      <View style = {styles.container}>

      <Input
        placeholder='Enter Name'
        onChangeText={(name) => this.setState({name})}/>

      <Input
        placeholder='Enter Email'
        onChangeText={(email) => this.setState({email})}/>

      <Input
        placeholder='Enter Password'
        onChangeText={(Password) => this.setState({Password})}/>

       <Input
        placeholder='Enter TC'
        onChangeText={(tc) => this.setState({tc})}/>

        <Input
         placeholder='Enter Phone'
         onChangeText={(phone) => this.setState({phone})}/>

    <View style={{marginTop : 40,flexDirection : 'row'}}>

     <Button
       title="Sign UP"
       onPress = {() => this.handleRegisterUser(this.state.name,this.state.email,this.state.Password,this.state.tc,this.state.phone)}/>
      </View>
     </View>
      );
  }
}

const styles = StyleSheet.create({
  container :
  {
    flex : 1,
    justifyContent : 'center',
    alignItems : 'center',
  }
});

export default App;

我在哪里犯错了?解决这个问题很久了,谢谢大家的帮助。

【问题讨论】:

    标签: javascript android node.js react-native


    【解决方案1】:

    handleRegisterUser() 中,您需要通过这种方式从状态中检索值:

    const {name,email,Password,tc,phone} = this.state;
    

    this.setState 是一个用于更新组件状态的函数。您可以使用this.state 访问状态。

    请参阅official doc 以供参考。

    【讨论】:

    • 现在出现新错误:函数 CollectionReference.doc() 要求其第一个参数为非空字符串类型,但它是:未定义
    • @chc 可能是因为您的 userID 未定义。尝试从 createUserWithEmailAndPassword 函数中捕获任何错误。
    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 2021-12-22
    • 2018-08-20
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2018-11-20
    相关资源
    最近更新 更多