How to query the existing children and loop over an Object to detect if it exists in Firebase using a Google Cloud Function?

后端 未结 1 1591
猫巷女王i
猫巷女王i 2021-01-29 01:50

I am trying to update (adding) some children on a firebase database. On this scenario I need to update a node without overwriting all the child nodes with Google Cloud Function

相关标签:
1条回答
  • 2021-01-29 02:17

    You could do as follow:

    let afterIns;
    exports.updateRoom = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
    
        const beforeData = change.before.val();  //data before
        const insBefore = beforeData.ins;
    
        const afterData = change.after.val(); // data after the write
        const roomPushKey = afterData.inRoom;
        afterIns = afterData.ins;
    
        return admin.database().ref().update(updates);
    
        const updates = {};
    
        Object.keys(ins).forEach(key => {
            if (insBefore.hasOwnProperty(key)) { // checking if the insBefore object has a key equal to value 'key'
                //True -> add an extra character at the existing key
                updates['/rooms/' + roomPushKey + '/ins/' + key + '_a'] = true;  //<- _a being the extra character
            } else {
                updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
            }
        });
    
        return admin.database().ref().update(updates);
    
    }).catch(error => {
        console.log(error);
        //+ other error treatment if necessary
    
    });
    

    Don't forget the function returns the data before change.before.val(); (in addition to the data after), therefore you don't have to do admin.database().ref().parentPath.on('value', function(snapshot) {


    EDIT after comments

    This should work, but I haven't test it. You may need to test is insBefore is undefined in addition to testing it is null. I let you fine tune it.

    let insAfter;
    let roomPushKey ;
    exports.updateRoomIns = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => {
        const afterData = change.after.val(); // data after the write
        roomPushKey = afterData.inRoom;
        insAfter = afterData.ins;
        return admin.database().ref('/rooms/' + roomPushKey).once('value').then(snapshot => {
        const insBefore = snapshot.val().ins;
        const updates = {};
        if (insBefore === null || insBefore === undefined ) {
            Object.keys(insAfter).forEach(key => {
               updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
            });
        } else {
            Object.keys(insAfter).forEach(key => {
               if (insBefore.hasOwnProperty(key)) {
                  updates['/rooms/' + roomPushKey + '/ins/' + key + '_a'] = true; 
               } else {
                  updates['/rooms/' + roomPushKey + '/ins/' + key] = true;
               }
            });
        }
        return admin.database().ref().update(updates);
        });
    });.catch(error => {
        console.log(error);
        //+ other error treatment if necessary
    
    });
    
    0 讨论(0)
提交回复
热议问题