问题
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