Ad

Why I Am Getting Uid As Undefined

I am calling uid for storage and it keep getting undefined whats wrong?

handleAdminSubmit(e) {
    e.preventDefault()

    const auth = firebase.auth();
    let promise = auth.createUserWithEmailAndPassword(this.state.SubEmail, this.state.subPasswrd);
    promise.then(user=> {
    console.log(user.uid)
      let email = this.state.SubEmail
      let em = email.replace('.com', '')
      let db = firebase.database();

      db.ref("Admins").child(em)
        .set({ "Admin": this.state.SubEmail, "password": this.state.subPasswrd, "Name": this.state.adminName  })
        .then(e => {
          console.log("succes")
          this.FetchAdmins()
        });


    })
    promise.catch(e => {
      alert("admin not created , Try again")
    })
  }
Ad

Answer

The type you get passed to the then is an UserCredential. To get the actual user, you have to access the user property on that. So:

auth.createUserWithEmailAndPassword(this.state.SubEmail, this.state.subPasswrd);
    .then(credential => {
        console.log(credential.user.uid)
Ad
source: stackoverflow.com
Ad