Firebase - How to update many children and not delete others in AngularFire

Viewed 1374

I want to use update() on firebase ref to update many children in one operation. To do this I passed the object with values to change.

Here is the output of console.log(angular.toJson(change,1))

{
  "10": {
    "otherRubies": 30
  },
  "11": {
    "otherRubies": 30
  }
} 

At the beginning i have:

enter image description here Then i do:

var refUsers = new Firebase(FBURL).child('users/');
refUsers.update(change);

So i want to have:

enter image description here

but instead of that i get:

enter image description here

Is there any way to do that ?

3 Answers

This is a sample of recursive update:

function updateRecursively(path, value) {
  for (let key in value) {
    if (value[key] instanceof Object) {
      updateRecursively(`${path}/${key}`, value[key]);
    } else {
      firebase.database().ref(`${path}/${key}`).set(value[key]);
    }
  }
}

Usage:

updateRecursively('users', {
  "10": {
    "otherRubies": 30
  },
  "11": {
    "otherRubies": 30
  }
})
Related