【问题标题】:App crashes when email or password are wrong with firebase当电子邮件或密码与 Firebase 错误时,应用程序崩溃
【发布时间】:2019-02-16 22:51:14
【问题描述】:

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

相关的代码行是:

    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 错误,我真的不明白为什么它不能正常工作。

非常感谢您的回答。

【问题讨论】:

  • auth.js 第 17 行等尝试找到调用调用的位置\

标签: javascript firebase react-native firebase-authentication


【解决方案1】:

您的代码似乎正确。

唯一可能的问题可能是 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 
        });
      }
  }
};

【讨论】:

  • 它说“没有与此标识符对应的用户记录。该用户可能已被删除。”
  • @AdarWallerstein 在我对console.debug 的回答中更改console.error 否则会显示红屏:)
猜你喜欢
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
  • 2019-08-03
  • 2015-06-09
  • 1970-01-01
相关资源
最近更新 更多