为什么功能从一开始就不起作用?

时间:2018-11-28 13:49:52

标签: javascript node.js firebase react-native firebase-realtime-database

我有以下两个文件:

db.js

...

export const checkEmailExistence = (email) => {

db = firebase.database();

  let statusArray = [],
      status = false;

  db.ref(tableName).on('value', (snapshot) => {
    if(snapshot.val()) {
      Object.keys(snapshot.val()).forEach(key => {
        if(email == snapshot.val()[key].email) {
          statusArray.push(1);
        }
      });
    }

    if(statusArray[0] === 1) {
      status = true;
    }
  }, (errorObject) => {
    console.warn('The read failed: ' + errorObject.code);
  });

  if(status) {
    return true;
  } else {
    return false;
  }
}

PageRegistration.js

...
validateData = (text, type) => {
    let nameAlph = /^[a-zA-Z]+$/;
    let emailAlph = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    switch(type) {

        ...

        case 'email':
            if(text == '' || !emailAlph.test(String(text).toLowerCase())) {
                this.setState({emailError: true});
            } else {
                if(checkEmailExistence(text)) {
                    this.setState({emailErrorMessage: true, emailError: true});
                } else {
                    this.setState({emailErrorMessage: false, emailError: false});
                }                 
                this.setState({emailError: false});
                this.setState({email: text});
            }
            break;

        ...
        }
    }

registerUser = () => {
    if(!this.state.firstname || !this.state.surname || !this.state.email || !this.state.password ||
        !this.state.repeatPassword || this.state.firstnameError || this.state.surnameError || 
        this.state.emailError || this.state.emailErrorMessage || this.state.passwordError || 
        this.state.repeatPasswordError || (this.state.password !== this.state.repeatPassword)) {
        this.setState({
            errorMessage: true
        });
    } else {
        this.setState({
            errorMessage: false
        });
        let user = {
            firstname: this.state.firstname, 
            surname: this.state.surname, 
            email: this.state.email, 
            password: this.state.password
        };
        addUser(user, () => {
            this.props.navigation.navigate('PageHome');
        });
    }

render() {
   return(
      <View style = {pageRegistrationStyle.container}>
           ...
          <TouchableOpacity onPress = {this.registerUser}
                    style = {pageRegistrationStyle.registrButton}>
                    <Text style = {pageRegistrationStyle.registrButtonText}>
                        ADD USER
                    </Text>
                </TouchableOpacity>
           ...

      </View>
    )
}
...

实际行为:

在我按下提交按钮调用函数 validateData()之后,除了 checkEmailExistence()之外,其他所有功能都正常运行。第一次启动应用程序时,此功能不会检查db中是否已经存在电子邮件。可以将具有相同电子邮件的用户添加到数据库。在尝试将textinput字段中的电子邮件更改为另一个已经存在的电子邮件之后,我最终收到对应文本的错误消息。因此,在第一次按下“提交”按钮后,此功能可以正常工作。

有人知道如何解决吗?

0 个答案:

没有答案