简体   繁体   中英

How to get child Snapshots inside snapshots on Google Cloud functions (Firebase)?

I'm trying to get some for Loops running inside a google cloud functions everytime I delete my /users node.

This is the code I'm using

exports.deleteUserAssets = functions.database.ref('/users/{userId}').onWrite((change, context) => {
const beforeData = change.before.val(); 
const afterData = change.after.val(); 
const userBuildings = Object.keys(beforeData.isAdmin); // get the  buildings of the user stored in the user/userId/isAdmin node ..  so far so good
const userId = beforeData.userIDforCloudFunctions; // I'm getting this from a /users/userid/userIDforCloudFucntions node ...so far so good (i've been logging it to confirm)
// making sure it was a delete operation ... so far so good
if (afterData !== null) {
    return 0;
} 
else {
    // on each building 
    for (var i = 0; i < userBuildings.length; i++) {
        let eachBuilding = [userBuildings[i]]
// HERE IS WERE THE PROBLEM IS: Trying to delete all depts + rooms + doors
        admin.database().ref('/buildings/' + eachBuilding)
            .child("hasDepts") 
            .once("value")
            .then(function(snapshot) { // This is where it goes south – snapshot is returning null
            snapshot.forEach(function(childSnapshot) {
                var deptKeyString = childSnapshot.key; // will try to get the keys of the departments stored under this space
                var deptsOnNode = admin.database().ref('/depts/' + deptKeyString);
                deptsOnNode.remove(); // and use the keys to delete each of the depts on depts
            });
        }); 

        admin.database().ref('/buildings/' + eachBuilding).set({}); // this is working
        admin.database().ref('/buildingsUserUid/' + userId +  '/' + eachBuilding).remove(); // this is working 
    }
}   
return 0;
});

The snapshot of admin.database().ref('/buildings/' + eachBuilding).child("hasDepts") is returning null.

How can I get to it? Besides admin.database().ref() I've tried to reach it with firebase.database().ref() which is the command/object i use to get this running on frontend functions. I've also tried functions.database() with no result.

Taking in consideration what Doug Stevenson mentioned in his second comment:

exports.deleteUserAssets = functions.database.ref('/users/{userId}').onDelete((change, context, event) => {
    const beforeData = change.before.val(); // data before the write (data of all the doors child nodes)
    const afterData = change.after.val(); // data before the write (data of all the doors child nodes)
    const userBuildings = Object.keys(beforeData.isAdmin); // get the  buildings of the user
    const userId = beforeData.userIDforCloudFunctions;
    // make sure user was deleted
    if (afterData !== null) {
        return 0;
    } 
    else {
        // on each building
        for (var i = 0; i < userBuildings.length; i++) {
            let eachBuilding = [userBuildings[i]]
          // Need to RETURN the whole chain of promises
            return admin.database().ref('/buildings/' + eachBuilding)
                .child("hasDepts")
                .once("value")
                .then(function(snapshot) {
                    console.log(snapshot.val()) // this now works
                    snapshot.forEach(function(childSnapshot) {
                        console.log(childSnapshot.val()) // this works as well
                        var deptKeyString = childSnapshot.key; // get the keys of the departments stored under this space
                        var deptsOnNode = admin.database().ref('/depts/' + deptKeyString);
                     // and you can keep on going deeper if you return promises
                        return deptsOnNode
                        .child('hasRooms')
                        .once('value')
                        .then(function(grandchildSnapshot){
                            console.log(grandchildSnapshot.val())
                            grandchildSnapshot.forEach(function(grandGrandchildSnapshot){

                            var roomKeyString = grandGrandchildSnapshot.key;
                            var roomsOnDepts =  admin.database().ref('/rooms/' + roomKeyString);
                            admin.database().ref('/roomOwners/' + userId +  '/' + roomKeyString).remove();
                         // and return again here...
                            return roomsOnDepts
                                .child('hasDoors')
                                .once('value')
                                .then(function(grandgrandGrandchildSnapshot){
                                grandgrandGrandchildSnapshot.forEach(function(grandgrandGrandchildSnapshot){
                                    var doorKeyString = grandgrandGrandchildSnapshot.key;
                                    var doorsOnRooms = admin.database().ref('/doors/' + doorKeyString);
                                    doorsOnRooms.remove();
                                    let clipOwners = admin.database().ref('/clipOwners/' + doorKeyString);
                                    clipOwners.remove();
                                })
                                roomsOnDepts.remove();
                            })         
                        })
                        deptsOnNode.remove(); // use the keys to delete the depts on depts main Node
                    })
                });
                admin.database().ref('/buildings/' + eachBuilding).set({}); 
                admin.database().ref('/buildingsUserUid/' + userId +  '/' + eachBuilding).remove();
            });           
        }
    }   
    return 0;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM