Ionic AngularFire 实时数据库 - 更改推送条目的默认 UID
Ionic AngularFire Realtime Database - Change Default UID of pushed entry
我正在开发一个 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);
});
}
现在,代码导致了这个结果:
我该如何解决这个问题?谢谢!
您可以使用 'set' 和您想要的 ID
,而不是使用生成 ID 的 'push'
this.db.object(`users/${newId}`).set({
admin: false,
identifier: data.user.email,
username: username,
});
我正在开发一个 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);
});
}
现在,代码导致了这个结果:
我该如何解决这个问题?谢谢!
您可以使用 'set' 和您想要的 ID
,而不是使用生成 ID 的 'push' this.db.object(`users/${newId}`).set({
admin: false,
identifier: data.user.email,
username: username,
});