创建用户在firebase中创建具有相同信息的未识别子项

时间:2018-05-03 01:36:52

标签: javascript firebase firebase-realtime-database firebase-authentication

我在firebase中创建了一个用户,其中包含一些额外的细节。我使用firebase的createUserWithEmailAndPassword,然后在具有该函数生成的相同身份验证ID的子项下添加其他详细信息。但是,当我这样做时,它还会生成一个子名称" undefined"使用相同的用户信息。

Picture of my database

以下是代码:

async register(user: User) {
    var id : string;
    try {
      //uses Firebase's Authentication Services to store email and password
      const result = await firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
      .then(function(response){
        console.log('uid',response.uid)
        id=response.uid;
      }

    ).catch(function(error) {
          //Handle error
      });
      console.log(result);
    }
    catch {error => {
      console.log("Error registering user",error);
    }}
    //creates user node with user's information
    console.log(id);
      this.user.type = "customer"
      firebase.database().ref().child(`User/${id}`).set(this.user);
  }

1 个答案:

答案 0 :(得分:2)

依赖于新创建的用户配置文件的所有代码都应该在创建用户后调用的回调中。所以:

firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
  .then(response => {
    id=response.uid;
    //creates user node with user's information
    this.user.type = "customer"
    firebase.database().ref().child(`User/${id}`).set(this.user);
  });
}).catch {error => {
  console.log("Error registering user",error);
}}