Reputation: 45
I'm having trouble lately adding the json structure i want into firebase database. I want to add an extra attribute to my database like in the image
I tried orderstReference.child(pid).child("quantity").setValue(orderId);
but the value is overwriting each time its execute where i want them to add like in a list.
How can i add this ? and is there any useful link to learn these stuff i can't find what i want?
Upvotes: 0
Views: 215
Reputation: 80914
If you already have a node with data, and you want to add an extra child then using setValue()
will override the whole node. In this case, you need to use updateChildren()
:
private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}
Check this link:
https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields
Upvotes: 1
Reputation: 313
Instead of set value which will override everything try update Children.
Upvotes: 0