Uncaught (in promise) TypeError: Cannot read property 'sendEmailVerification' of null

1,085 views
Skip to first unread message

Sreedar Raju

unread,
May 31, 2020, 10:46:06 AM5/31/20
to Firebase Google Group
Hi all
I am still new to firebase, fumbling along. I am stuck with an issue with SendEmailVerification call.
When I run the code I get

Uncaught (in promise) TypeError: Cannot read property 'sendEmailVerification' of null

var promise = auth.createUserWithEmailAndPassword(emailAddress, password);
firebase.auth().onAuthStateChanged(function(user) {
      user.sendEmailVerification();
      console.log("inside of sendEmailVerification");
      window.location.replace("/ChkYourEmail.html");
    });

Im not sure what I am doing wrong here. Can someone please point out?
Appreciate your responses.
thanks
Sreedar

Frank van Puffelen

unread,
May 31, 2020, 10:50:48 AM5/31/20
to Firebase Google Group
Hey Sreedar,

The onAuthStateChanged gets called both when the user is signed in, and when they're not signed in yet, or when they signed out. So the code you have inside the callback will always need to check if user has a value:

    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        user.sendEmailVerification();
        console.log("inside of sendEmailVerification");
        window.location.replace("/ChkYourEmail.html");
      }
    });

You'll probably also want to check if the user has already verified their email address:

      if (user && !user.emailVerified) {
        user.sendEmailVerification();
        ...

Sreedar Raju

unread,
May 31, 2020, 6:18:05 PM5/31/20
to fireba...@googlegroups.com
Thank you Frank for the reply. 
I did that, been trying for 2-3 days now, but I have to admit that I am a noob at this. But I tried many things, you can see from the commented code below.
When I use the code you mentioned, the SendEmailVerification just doesn't get called - because user is null. To me, this simply suggests that the createUserWithEmailAndPassword may not be working - but the account does get created, I see that in the firebase console. That could mean only one thing - that the user account gets created but the automatic sign in is not happening.

What am I doing wrong?
Thanks
Sreedar

function submitSignup(){
  console.log("Called submitSignup");
  // event.preventDefault(); // firefox doesnt work without this.
  var username = $("#applyUserName").val();
  var password = $("#password").val();
  var emailAddress = $("#applyEmail").val();
  var btnLogin = document.getElementById('signinbutton');
  var btnSignUp = document.getElementById('Verification'); // these two should be bunched with an "or"
  var btnSignUp1 = document.getElementById('signupbutton');//
  var btnLogout = document.getElementById('signoutbutton');
  console.log("username: "+username+" password: "+password+" Email Addr: "+ emailAddress);
  if (username == "" || password == "" || emailAddress == "") {
      //alert("Please fill all details")
      alertbox("error", "All fields are mandatory");
      return false
  } else {
    auth.onAuthStateChanged(function(user) {
    if (user) {
      firebase.auth().signOut(); //if a user is signed in, sign them out
    }
    });
    console.log("running createUserWithEmailAndPassword");

    var promise = auth.createUserWithEmailAndPassword(emailAddress, password);
    //  .catch(function(error) {
    //   // Handle Errors here.
    //   var errorCode = error.code;
    //   var errorMessage = error.message;
    //   console.log("Error creating user: "+errorCode+" Message: "+errorMessage);
    //   alertbox("Error creating signup user, error: "+errorMessage);
    //   return false;
    // });
    console.log("Done with createUserWithEmailAndPassword");
    console.log("Start of sendEmailVerification");
    var user = firebase.auth().currentUser;
    // promise.then(function(user) {
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {

        if (user && !user.emailVerified) {
        user.sendEmailVerification();
        console.log("inside of sendEmailVerification, sending mail");
        // window.location.replace("/ChkYourEmail.html");
        $(location).attr('href', '/ChkYourEmail.html');
      }else{
        console.log("inside of sendEmailVerification, user already verified");
      }
      console.log("inside of sendEmailVerification, user not sigened in");
      }
    });
    console.log("outside of sendEmailVerification");
  }


Console messages:

Called submitSignup
VM188 script.js:412 username: sree1234 password: sr331234 Email Addr: sre...@gmail.com
VM188 script.js:423 running createUserWithEmailAndPassword
VM188 script.js:434 Done with createUserWithEmailAndPassword
VM188 script.js:435 Start of sendEmailVerification
VM188 script.js:451 outside of sendEmailVerification
Navigated to https://qa.slr67.net/ind_signup?applyUserName=sree1234&password=sr331234&applyEmail=sreedar%40gmail.com&Verification=
ind_signup?applyUserName=sree1234&password=sr331234&applyEmail=sreedar%40gmail.com&Verification=:386

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/656bfe9d-2142-4259-a86d-c1c42a03b6ba%40googlegroups.com.

Frank van Puffelen

unread,
Jun 1, 2020, 11:54:55 AM6/1/20
to Firebase Google Group
Two things you can do to troubleshoot:

1. Reenable the .catch() on createUserWithEmailAndPassword(...) as it may show important information about a failure.
2. Add a console.log("auth state changed: user="+user) as the first line inside your onAuthStateChanged callback

If you can't get it to work with that, I recommend setting up a reproduction on a site like jsbin, stackblitz or similar, so that we can have a look there.

    puf
To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages