0

I have the following code; it successfully creates the user and returns a promise. According to the documentation, it returns a non-null user upon completion, however, as i try to print out the email or send a verification email i get "undefined" and "sendVerificationEmail is not a function"

function register(){
    var email = document.getElementById('email').value;
    var password = document.getElementById('pass').value;
    
    var promise = firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
          // Handle Errors here.
          var errorCode = error.code;
          var errorMessage = error.message;
          // ...
        });
        
    if(promise != null){
      alert(promise.email);
      promise.sendEmailVerification();
      
    }  
    
}

any ideas?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Nathan
  • 163
  • 10
  • http://stackoverflow.com/questions/37431128/firebase-confirmation-email-not-being-sent This thread may help, I think you need to access the api before you use the sendemailverification api call. – rob Oct 21 '16 at 15:54

1 Answers1

0

This isn't how you access a promise. I'm not super familiar with the underlying Firebase code, but assuming they are using an A+ compliant promise solution then there should be a positive counterpart to the .catch() method. Its typically .then().

So what that means is that you need to actually wait until the promise resolves before you can access what it is returning.

So instead of promise.email you would do something like this.

promise.then(function(rsp) {
  window.alert(rsp);
});

I also think it might make it easier if you were to ditch the whole promise variable. And do something like the following...

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(function(rsp) {
   // do something with rsp
  })
  .catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // ...
  });

Then it would be a bit more obviously what is going IMO.

I would recommend looking into promises a bit more here at www.promisejs.org

Scott Sword
  • 4,648
  • 6
  • 32
  • 37