如何获取UID
等于firebase.auth().currentUser.uid
的密钥,因为当用户输入新数据时我可以更新此信息。
如何更新信息?我使用orderByChild
并等于和设置,但不起作用。
我的代码:
updateUserInformation() {
var ref = firebase.database().ref('/users/');
ref.on('value', (snapshot) => {
snapshot.forEach((child) => {
if (child.val().UID == firebase.auth().currentUser.uid) {
this.profileData = [{
name: child.val().name,
lastname: child.val().lastname,
phone: child.val().phone,
direction: child.val().direction,
followQuantity: child.val().followQuantity,
points: child.val().points,
sex: child.val().sex,
}];
this.cdRef.detectChanges();
}
});
})
}
答案 0 :(得分:1)
假设您的实时数据库中有唯一的UID
字段,则可以执行以下操作:
updateUserInformation() {
const uid = firebase.auth().currentUser.uid;
var ref = firebase.database().ref('/users/').orderByChild('UID').equalTo(uid);
ref.once('value', (snapshot) => {
const updates = {};
snapshot.forEach((child) => {
const userKey = child.key;
const userObject = child.val();
updates[`${userKey}/fieldWhichYouWantToUpdate`] = `Field Value you want it to set to`;
firebase.database().ref('/users/').update(updates);
});
})
}
我还没有测试过,但是我认为这应该可行。
答案 1 :(得分:0)
这里是Firestore和ES6 Promises的示例。如果您考虑切换到Firestore,查询将如下所示:
Karabiner
答案 2 :(得分:0)
@SiddAjmera的回答是正确的。它还显示了当前数据模型的一个缺点:可能有多个具有正确UID的节点。尽管您的应用程序可能不允许这样做,但您的数据模型却允许这样做,并且您无法通过安全规则强制实施UID的唯一值。
每当您存储具有自然标识符的项目时,请考虑使用该自然ID作为其关键字来存储这些项目。对于用户而言,这意味着您应该强烈考虑将用户存储在其UID中。
users: {
"BD21...Wv33": {
"email": "test2@test.com",
...
}
}
此结构:
您只需使用以下用户的UID查找用户即可:
var ref = firebase.database().ref('/users/').child(uid);
ref.once('value', (snapshot) => {
console.log(snapshot.key);
snapshot.ref.child("fieldWhichYouWantToUpdate"].set("Field Value you want it to set to");
})