【问题标题】:Ionic AngularFire Realtime Database - Change Default UID of pushed entryIonic AngularFire 实时数据库 - 更改推送条目的默认 UID
【发布时间】:2018-11-17 12:31:40
【问题描述】:

我正在开发一个 Ionic 应用程序,除其他事项外,该应用程序应分配一个相应的数据库条目,其密钥与他们在注册时获得的用户生成的 UID 相同。另外需要注意的是,用户自己不能创建帐户 - 只有单个管理员可以。

这是我当前的代码。

addUserToDb(username: string, email: string, password: string) {
    this.firebaseAuth.auth.createUserWithEmailAndPassword(email, password).then(data => {
    var newId = data.user.uid; // Gets UID of new user.
    const usersRef = this.db.list('users'); // Gets list to be pushed to
    let keyToChange = usersRef.push(
    {
        admin: false,
        identifier: data.user.email,
        username: username,
    }).key; // Does the push AND gets the key for the newly added entry
    console.log(`User's old id is ` + keyToChange + " | User's new id will be " + newId);
    var userToUpdate = this.db.object('users/' + keyToChange); // Gets entry to be edited
    userToUpdate.update({[keyToChange]: newId}); // <- WHAT DO I DO HERE?
    console.log('Success!', username);
  })
  .catch(err => {
    username: null;
    email: null;
    password: null;
    console.log('Something went wrong:', err.message);
  });    
}

现在,代码导致了这个结果:

我该如何解决这个问题?谢谢!

【问题讨论】:

    标签: javascript ionic-framework firebase-realtime-database nosql angularfire


    【解决方案1】:

    您可以将“set”与您想要的 ID 一起使用,而不是使用生成 ID 的“push”

     this.db.object(`users/${newId}`).set({
        admin: false,
        identifier: data.user.email,
        username: username,
     });
    

    【讨论】:

    • 完美运行!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多