29

I want to update my display name in the code below. How can I update displayName?

My database structure is:

-Users
      -KUanJA9egwmPsJCxXpv

         displayName:"Test Trainer"

         email:"test@gmail.com"

         uid: "jRXMsNZHR2exqifnR2rXcceEMxF2"
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Nishith Adhvaryu
  • 427
  • 2
  • 6
  • 12
  • 1
    Your database should look like a JSON, the format you're giving is confusing. And I can't see any code. – Elfayer Nov 14 '16 at 13:12

3 Answers3

81

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").orderByChild("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 Unique ID:

-Users
      jRXMsNZHR2exqifnR2rXcceEMxF2
         displayName:"Test Trainer"    
         email:"test@gmail.com"

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" });
Jellyfish
  • 304
  • 3
  • 13
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

How to check whether it's updated or not?

Answer: https://stackoverflow.com/a/44123067/3156040

I was looking for that as well.

user3156040
  • 723
  • 5
  • 5
0

Use this code to update any child of database

-Users
      -KUanJA9egwmPsJCxXpv

         displayName:"Test Trainer"

         email:"test@gmail.com"

         uid: "jRXMsNZHR2exqifnR2rXcceEMxF2"

Database reference = FirebaseDatabase.getInstance().getReference("Users").child("KUanJA9egwmPsJCxXpv");
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("email", newValue);
                reference.updateChildren(hashMap);
Ankit Verma
  • 496
  • 5
  • 21