TypeError:尽管函数返回promise,但无法读取未定义的属性“ then”

时间:2020-08-12 12:39:44

标签: reactjs es6-promise arrow-functions

class Firebase {
  constructor() {
    app.initializeApp(firebaseConfig);
    this.auth = app.auth();
  }

  doCreateUserWithEmailAndPassword = (email, password) => {
   this.auth.createUserWithEmailAndPassword(email, password);
  };
}

Firebase.doCreateUserWithEmailAndPassword()调用.then()方法时,我得到以下错误方法:

TypeError:无法读取未定义的属性“ then”

1 个答案:

答案 0 :(得分:1)

我认为许多初学者都面临这个问题,所以我想分享我的解决方案。

由于箭头函数的语法,应该删除doCreateUserWithEmailAndPassword方法周围的{},以便返回this.auth.createUserWithEmailAndPassword()的结果

我本可以保留{}并添加一个return语句。

所以正确的方法应该是:

 doCreateUserWithEmailAndPassword = (email, password) => 
    this.auth.createUserWithEmailAndPassword(email, password);

 doCreateUserWithEmailAndPassword = (email, password) => {
    return this.auth.createUserWithEmailAndPassword(email, password);
  };