Biswanath Maity
Biswanath Maity

Reputation: 170

How to update child with new fields in firebase realtime database?

During authentication the user is created with email and user name. Now I am trying to update that user with new fields like first name, last name, address, etc on registration time. But when I try to insert a new field it updates with the new fields and the old fields are removed.

public class User {
        String uid,userName,firstName,lastName,email;

        public User() {
        }
       //called on the time of auth 
       public User(String email, String userName) {
            this.email = email;
            this.userName = userName;
        }
        //called on registration process 
        public User( String firstName, String lastName,String mobileNo) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.mobileNo = mobileNo;
        }

        @Exclude
        public Map<String, Object> toMap() {
            HashMap<String, Object> result = new HashMap<>();
            result.put("email", email);
            result.put("userName", userName);
            result.put("firstName", firstName);
            result.put("lastName", lastName);
            return result;
        }

The following methods are used to add and update the firebase database. The addUser method is functional properly but during the update method it replace the old data.

String userId = getUid(); // its retrun firebase current user id as I use 
                          //  auth authentication     
//first time entry in database
private void writeNewUser(String name, String email) {
     User user = new User(name, email);
     Map<String, Object> postValues = user.toMap();
     mDatabase.child("users").child(userId).setValue(postValues);
} 
//Its called during the registration porecess 
private void updateUser() { 
       User user = new User(firstName, lastName, email);
       Map<String, Object> postValues = user.toMap();
       mDatabase.child("users").child(userId).updateChildren(postValues);
}

Upvotes: 3

Views: 13517

Answers (2)

Roma Debrian
Roma Debrian

Reputation: 21

I don't know for java
in javascript to update the token on the profile, i use this way for edit value "Token" :

const db = getDatabase();
const postData = "123456789";
const updates = {};

updates["users/" + userID + "/" + "Token"] = postData;

return update(ref(db), updates)

with a data structure in firebase like this :

{
"users": {
    "UuhktDC8QmaWZZtEYbFHFS7cOsK3" : {
        "Name" : "Roma",
        "Email" : "[email protected]",
        "Token" : "faaQ7aRkTNCGOTG"
    }
  }
}

Upvotes: 0

Faruk
Faruk

Reputation: 5821

I think the solution is quite simple, just get the old value first, before update, and modify with new fields or new value, then do the update.

To get the oldValues I don't know if using getValue(User.class) will return error or not, so for safety, let's just loop from the children.

private void updateUser() { 
   mDatabase.child("users").child(userId)
     .addListenerForSingleValueEvent(new ValueEventListener() {
       @Override
       public void onDataChange(DataSnapshot dataSnapshot) {
          Map<String, Object> postValues = new HashMap<String,Object>();
          for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
             postValues.put(snapshot.getKey(),snapshot.getValue());
          }
          postValues.put("email", email);
          postValues.put("firstName", firstName);
          postValues.put("lastName", lastName);
          mDatabase.child("users").child(userId).updateChildren(postValues);
       }

       @Override
       public void onCancelled(DatabaseError databaseError) {}
       }
    );
}

And also, the constructor that you wrote for new User(String,String,String) is for firstName, lastName, and mobileNo Is the field that gone is perhaps the email?

Upvotes: 4

Related Questions