如果Firebase数据库部分中的数据未定义,我如何更新Profile?

时间:2019-04-05 18:47:26

标签: javascript react-redux firebase-authentication redux-form

我无法克服此错误 TypeError: createdUser.updateProfile is not a function at _callee2$ (authActions.jsx:22) 通过Firebase设置我的注册表格时。

当我删除代码中带有注释的“更新身份验证配置文件”部分时,会通过注册表单,但是我的文档在Firebase数据库部分中未定义。

export const registerUser = (user) => 
  async (dispatch, getState, {getFirebase, getFirestore}) => {
    const firebase = getFirebase();
    const firestore = getFirestore();
    try {
      // create the user in firebase auth
      let createdUser = await 
      firebase.auth().createUserWithEmailAndPassword(user.email, user.password);
    //update the auth profile
    await createdUser.updateProfile({
      displayName: user.displayName
    }) 
    // create a new profile in firestore
    let newUser = {
      displayName: user.displayName,
      createdAt: firestore.FieldValue.serverTimestamp()
    }
    await firestore.set(`users/${createdUser.uid}`, {...newUser})
    dispatch(closeModal());
  } catch (error) {
    console.log(error)
    throw new SubmissionError({
     _error: error.message
    })
  }
}

2 个答案:

答案 0 :(得分:0)

createUserWithEmailAndPassword返回一个以UserCredential解析的承诺。

您需要更改为以下内容:

let userCredential = await 
    firebase.auth().createUserWithEmailAndPassword(user.email, user.password);
//update the auth profile
await userCredential.user.updateProfile(...);

答案 1 :(得分:0)

 firebaseService.auth().createUserWithEmailAndPassword(email,password).then((user) => {
    this.setState({message:'User Account created successfully', showExam:true});
    })
    .then(() =>{
      firebaseService.auth().onAuthStateChanged(user => {
        if (user) {
          console.log(this.state.lastName);
          user.updateProfile({ // <-- Update Method here
            displayName: firstName + " " + lastName,
          }).then(() =>{
            console.log("click the link in your email to verify your account");
          }, error => {
            console.log("update failed",error);
          });     
        }
      });
    })