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

问题描述

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

  docreateuserWithEmailAndPassword = (email,password) => {
   this.auth.createuserWithEmailAndPassword(email,password);
  };
}

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

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

解决方法

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

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

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

所以正确的方法应该是:

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

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