使用Node.js作为管理员在Firebase中更新值 [英] Updating a value in firebase using nodejs as admin
问题描述
var ref=db.ref("users");
var upref = ref.child("-KXpWvL3cgzHvFYgArTW");
upref.update({
"name": "Ayaz"
});
我正在使用它创建密钥的引用,但这不是正确的方法,在我的数据库中,我有用户,然后在我有名称的情况下有密钥.在这里,我将直接创建对密钥的引用并对其进行更新.我的数据库的结构是:
I m using this I am creating a reference to the key but this is not the right way , in my db i have users and then i have keys under that i have the name. Here I am creating a reference directly to the key and updating it. the structure of my db is:
用户
....->键
.............->名称:"
............. -->name:""
那么我该如何更新特定名称??
So how can I update a specific name.?
推荐答案
您需要知道要更新的用户的属性.例如,如果您知道要更新的用户名,则可以通过执行以下操作来更新用户:
You'll need to know a property of the user that you want to update. For example if you know the name of the user(s) that you want to update, you can update the user by doing:
var query = ref.child("users").orderByChild("name").equalTo("Ayz");
query.once("value", function(snapshot) {
snapshot.forEach(function(userSnapshot) {
userSnapshot.ref().update({ name: "Ayaz" });
});
});
需要循环( snapshot.forEach
),因为查询可以匹配多个子节点,所以结果将是匹配用户的列表.
The loop (snapshot.forEach
) is needed, since a query can match multiple child nodes, so the result will be a list of matching users.
请注意,习惯上将用户存储在用户的 uid
下,而不是像这样将用户存储在推送ID下.通过将它们存储在其 uid
下,通常可以避免进行上述查询.
Note that it is custom to store users under their uid
, instead of under a push ID as you've done. By storing them under their uid
, you can often prevent the need for the query above.
这篇关于使用Node.js作为管理员在Firebase中更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!