Nishith Adhvaryu Nishith Adhvaryu - 3 years ago 165
Javascript Question

Firebase DB - How to update particular value of child in Firebase Database

I want to update my display name in below code. How can I update my displayName.
My database structer is as below:


displayName:"Test Trainer"


uid: "jRXMsNZHR2exqifnR2rXcceEMxF2"

Answer Source

If you want to update the displayName of this user:

var db = firebase.database();
db.ref("-Users/-KUanJA9egwmPsJCxXpv/displayName").set("New trainer");

Alternatively, you can also get the same with:

db.ref("-Users/-KUanJA9egwmPsJCxXpv").update({ displayName: "New trainer" });

But it's likely you don't know the ID of the user, in which case you need to look that up first:

var query db.ref("-Users").orderByKey("uid").equalTo("jRXMsNZHR2exqifnR2rXcceEMxF2");
query.once("child_added", function(snapshot) {
  snapshot.ref.update({ displayName: "New trainer" })

One final remark on your data structure though: you seem to be storing user profiles, but you're storing them under a push ID. For this type of structure we usually recommend that you store each user under their UID:

         displayName:"Test Trainer"    

With such a structure you remove any chance that you're storing the same user twice. Plus, you can now update the user's display name without needing a query:

var currentUser = firebase.auth().currentUser;
db.ref("-Users/"+currentUser.uid).update({ displayName: "New trainer" });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download