Search code examples
angulartypescriptfirebasefirebase-realtime-databaseangularfire

Batch update works as set operation


I need to do a batch update with Firebase's real-time database. I have done it like so:

This is working fine. But I need an update only for the 2nd operation. At this moment it works as set operation. Any clue?

 const updates = {}; //batch operation

   // this is working fine and I need it as set operation
    updates[`groups/${this.groupId}/leads/${lead.id.toString()}`] = {
      name: lead.name,
      address: lead.address,
    };

    // But problem is here. I need to write it as update operation
    updates[`groups/${this.groupId}/addresses/${lead.id.toString()}`] = {
      address: lead.address,
     };

    return this.angularFireDatabase.object('/').update(updates);

Solution

  • A multi-path update functions as a set on each individual key in the map that you pass in. Since you pass in groups/${this.groupId}/addresses/${lead.id.toString()} as the key, it sets the value you specify on that path.

    If you only want to update the address property under that path, include the address key in the path, instead of in the value:

    updates[`groups/${this.groupId}/addresses/${lead.id.toString()}/address`] = lead.address;