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

I want to use update () for firebase ref to update many children in one operation. To do this, I passed an object with the values ​​that need to be changed.

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

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

In 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 I get:

enter image description here

Is there any way to do this?

+4
source share
3 answers

. . /, , / , , /.

:

var refUsers = new Firebase(FBURL).child('users/');
for(key in change) { 
   if( change.hasOwnProperty(key) ) {
      refUsers.child(key).update( change[key] );
   }
}
+5

Firebase 3 , :

update = {};
update['10/otherRubies'] = 30;
update['11/otherRubies'] = 30;
refUsers.update(change);

, .

0

This is an example of a 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]);
    }
  }
}

Using:

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

All Articles