【问题标题】:Firebase Auth Is Giving Me An Error When Trying To Resolve A Promise尝试解决承诺时,Firebase Auth 给了我一个错误
【发布时间】:2019-03-20 05:56:29
【问题描述】:

我需要有关此 Google 身份验证功能的帮助...我在 firebase.js (React) 中有一个方法 doCreateUserWithEmail 和密码,如下所示。

doCreateUserWithEmailAndPassword = (email, password) => {
 this.auth
  .createUserWithEmailAndPassword(email, password)
  .then(response => console.log(response))
  .catch(err => console.log(err));};

当我把 then .then 和下面的并将其附加到 signUp.js 时......

onSubmitHandler = event => {
event.preventDefault();
const { email, passwordOne } = this.state;
this.props.firebase.doCreateUserWithEmailAndPassword(email, passwordOne)
  .then(response => console.log(response))
  .catch(err => console.log(err));
this.props.history.push(ROUTES.HOME);

};

我收到此错误..

TypeError: Cannot read property 'then' of undefined
SignUpFormBase._this.onSubmitHandler
src/components/signUp/signUp.js:31
 28 | onSubmitHandler = event => {
 29 |   event.preventDefault();
 30 |   const { email, passwordOne } = this.state;
 > 31 |   this.props.firebase.doCreateUserWithEmailAndPassword(email, 
 passwordOne)
 | ^  32 |     .then(response => console.log(response))
 33 |     .catch(err => console.log(err));
 34 |   this.props.history.push(ROUTES.HOME);
 View compiled

是的,当我收到错误消息时,我从 doCreateUserWithEmailAndPassword 中删除了它。 但是该函数确实在 Firebase 中成功注册了用户,如果我拿走 .then 和 .catch 它就可以正常工作。

为什么会出现这个错误?

【问题讨论】:

    标签: javascript reactjs firebase-authentication runtime-error


    【解决方案1】:

    您没有从 doCreateUserWithEmailAndPassword 返回任何内容,因此它返回 undefined 并在 undefined 上调用 .then() 会导致您看到的错误。

    只需从doCreateUserWithEmailAndPassword 返回Promise 即可解决问题:

    doCreateUserWithEmailAndPassword = (email, password) => {
      return this.auth
        .createUserWithEmailAndPassword(email, password)
        .then(response => console.log(response))
        .catch(err => console.log(err));
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-11
      • 2015-12-19
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      相关资源
      最近更新 更多