尝试解决承诺时,Firebase Auth给我一个错误 [英] Firebase Auth Is Giving Me An Error When Trying To Resolve A Promise

查看:107
本文介绍了尝试解决承诺时,Firebase Auth给我一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关此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删除,它将可以正常工作.

为什么会出现此错误?

解决方案

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

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

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

I need help with this Google Auth Function… I have a method doCreateUserWithEmail and password in firebase.js (React) that looks like this.

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

When I take then .then and below and and attach it to signUp.js like so…

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

};

I get this error..

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

and yes, I deleted it off of doCreateUserWithEmailAndPassword when I get the error.. but the function does register the user in Firebase successfully, and if I take away the .then and .catch it works fine.

Why do I get this error?

解决方案

You aren't returning anything from doCreateUserWithEmailAndPassword so it returns undefined and calling .then() on undefined causes the error you are seeing.

Just return the Promise from doCreateUserWithEmailAndPassword and that should fix it:

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

这篇关于尝试解决承诺时,Firebase Auth给我一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆