Firebase的电子邮件或密码错误时,应用程序崩溃

时间:2019-02-16 22:51:14

标签: javascript firebase react-native firebase-authentication

所以我使用的是最新版本的 react native firebase + react-native-form-validator 。 当我尝试使用具有正确的电子邮件和密码的现有用户登录时,一切都很好,但是,当我输入错误的电子邮件或密码时,应用程序崩溃,并且出现此错误:

c.call is not a function

相关的代码行是:

    state = { email: '', password: '', error: '', loading: false }

    userLogin = () => {
            this.setState({ loading: true });

            this.validate({
                email: { required: true, email: true },
                password: { required: true, minlength: 5 }
            });

            if (this.isFormValid()) {
                firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
                .then(() => Actions.partyzmain())
                .catch(setTimeout(() => {
                    this.setState({ error: 'Email or password are inccorect!', password: '', loading: false });
                }, 5000));                 
            }
            else {
                if (this.isFieldInError('password')) {
                    this.setState({ error: 'Wrong password!', password: '', loading: false });
                }
                if (this.isFieldInError('email')) {
                    this.setState({ error: 'Invalid email adress!', loading: false });
                }
            }
    };

我设置了.catch错误,我真的不明白为什么它不能正常工作。

非常感谢您的回答。

1 个答案:

答案 0 :(得分:0)

您的代码似乎正确。

唯一可能的问题可能是setTimeout在不同的范围内运行this.setState,正如我在错误屏幕中看到的那样,该屏幕显示了带有计时器的回溯。

我建议您使用async/await并使用try...catch构造来处理所有可抛物。

对于计时器,您可以使用以下方法:

const wait = (timer) => 
  new Promise((resolve) => {
    setTimeout(resolve, timer);
  })

,总代码为:

state = { email: '', password: '', error: '', loading: false }


userLogin = async () => {
  try {
        this.setState({ loading: true });

        this.validate({
            email: { required: true, email: true },
            password: { required: true, minlength: 5 }
        });

        if (!this.isFormValid()) {
            if (this.isFieldInError('password')) {
                this.setState({ 
                  error: 'Wrong password!', 
                  password: '', loading: false 
                });
            }

            if (this.isFieldInError('email')) {
                this.setState({ 
                  error: 'Invalid email address!', 
                  loading: false 
                });
            }
            return;
        }

        await firebase
                .auth()
                .signInWithEmailAndPassword(
                  this.state.email, 
                  this.state.password
                );
        Actions.partyzmain();
  }
  catch (error) {
      console.debug(error);

      if (error.code.startsWith('auth')) {
        await wait(1000);
        this.setState({ 
          error: 'Email or password are inccorect!', 
          password: '', 
          loading: false 
        });
      }
  }
};