Updating a value in firebase using nodejs as admin

喜你入骨 提交于 2021-02-08 10:40:35

问题


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:

users

.... -->key

............. -->name:""

So how can I update a specific name.?


回答1:


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" });
    });
});

The loop (snapshot.forEach) is needed, since a query can match multiple child nodes, so the result will be a list of matching users.

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.



来源:https://stackoverflow.com/questions/41219543/updating-a-value-in-firebase-using-nodejs-as-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!