尝试解决承诺时,Firebase Auth给我一个错误

时间:2019-03-20 05:56:29

标签: javascript reactjs firebase-authentication runtime-error

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

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

当我接.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删除,它将可以正常工作。

为什么会出现此错误?

1 个答案:

答案 0 :(得分:1)

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

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

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