我正在开发一个 Ionic 应用程序,除其他外,该应用程序应该分配一个相应的数据库条目,其 key 与用户注册时生成的 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);
});
}
现在,代码会导致以下结果:
我该如何解决这个问题?谢谢!
最佳答案
您可以使用带有所需 ID 的“set”,而不是使用生成 ID 的“push”
this.db.object(`users/${newId}`).set({
admin: false,
identifier: data.user.email,
username: username,
});
关于javascript - Ionic AngularFire 实时数据库 - 更改推送条目的默认 UID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50743822/