Skip to content Skip to sidebar Skip to footer

How To Update A Node Without Overwriting All The Child Nodes With Google Cloud Function Using A Update Object?

I am updating 'Rooms/{pushId}/ins' with a Google Cloud function that gets new In data from several 'doors/{MACaddress}/ins'. The function currently goes like like this: exports.upd

Solution 1:

Elaborating from my previous answer to your related question (How to clone a node to another path based on a reference value from the initial path on Google Cloud Functions?), I would adapt the code as follow:

exports.updateRoom = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
    const afterData = change.after.val(); // data after the writeconst roomPushKey = afterData.inRoom;
    const ins = afterData.ins;

    const updates = {};

    Object.keys(ins).forEach(key => {
        updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
    });

    return admin.database().ref().update(updates);

}).catch(error => {
    console.log(error);
    //+ other rerror treatment if necessary

});

In other words, instead of replacing the full ins object, you add new children nodes in the existing ins node.

Post a Comment for "How To Update A Node Without Overwriting All The Child Nodes With Google Cloud Function Using A Update Object?"