Firebase 身份验证在尝试解决Promise时给我一个错误

IT技术 javascript reactjs firebase-authentication runtime-error
2021-05-02 06:02:31

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

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

当我将 then .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个回答

不必返回从任何doCreateUserWithEmailAndPassword所以它返回undefined,并呼吁.then()undefined造成您所看到的错误。

只需返回Promisefrom 即可doCreateUserWithEmailAndPassword修复它:

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