Luke Pighetti
Luke Pighetti

Reputation: 4841

How to update without overwriting children?

consider this existing data in a Firebase Realtime Database

db.ref('path/to/child').once('value')

{
  "data":{
    "this":"value",
    "that":"value"
  }
}

consider this set operation to the original data

db.ref('path/to/child').update({"data":{"this":"newValue"}})

{
  "data":{
    "this":"newValue",
  }
}

how do we perform an update so that the unchanged data is preserved

db.ref('path/to/child').foo({"data":{"this":"newValue"}})

{
  "data":{
    "this":"newValue",
    "that":"value"
  }
}

Upvotes: 0

Views: 1446

Answers (2)

Brandon Waring
Brandon Waring

Reputation: 103

I had the same issue but the example above didnt work. I came to the same conclusion based on the docs which also didn't work. It still overwrote the content in "data".

Testing with a slash after data (in my case the object key) worked for me. Maybe I'm still misunderstanding something about how this works, but thought I'd add this incase anyone else hit the same issue. If I'm wrong, please explain.

This did work exactly as I expected.

db.rev('path/to/child/data/').update({"this": "newValue")
// almost exact path I use on some random test data
// path -> root/userid/list/listid/

db.rev('user/2FsJBrxZHQFff61QkMIuTV49KBUcQ2/list/MOwh_FwDmlKbxsfUmZS/').update({"name": "newValue")

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317362

Right now, your code is telling Firebase to update the entire data child under path/to/child. Instead, you should reference the most deep path to the final child node you want to update:

db.ref('path/to/child/data').update({"this":"newValue"})

Note that I've put data in the reference, instead of the object to update.

Upvotes: 3

Related Questions