Michael
Michael

Reputation: 137

Firebase Auth Is Giving Me An Error When Trying To Resolve A Promise

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?

Upvotes: 1

Views: 641

Answers (1)

Brian Adams
Brian Adams

Reputation: 45800

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

Upvotes: 2

Related Questions