【问题标题】:Creating user creates unidentified child with same information in firebase创建用户在firebase中创建具有相同信息的身份不明的孩子
【发布时间】:2018-05-03 01:36:52
【问题描述】:

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

代码如下:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2020-06-10
      • 2019-05-01
      • 1970-01-01
      • 2016-07-31
      相关资源
      最近更新 更多