为什么我得到的uid不确定

时间:2019-07-10 20:08:07

标签: reactjs firebase web firebase-authentication

我正在调用uid进行存储,并且它不断得到未定义的错误?

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

2 个答案:

答案 0 :(得分:0)

uid对象上有auth

const auth = firebase.auth();
let promise = auth.createUserWithEmailAndPassword(this.state.SubEmail, this.state.subPasswrd);
promise.then(() => {
    console.log(auth.uid)
    ...

答案 1 :(得分:0)

您传递给then的类型是UserCredential。要获得实际用户,您必须访问该用户的user属性。所以:

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