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);
  };

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...