0
votes

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.

1
admin.database().ref(...).child(...) will not return null. There is no such thing as a null reference. A query snapshot will also not be null. If you're getting an error, please edit the question to post the error. If you're certain about the contents of some variable, please describe how you know its value (with console.log).Doug Stevenson
Another thing, and maybe the most important - you're not doing anything with any of the promises returned by the database API calls you're making. This means your function may appear to do nothing at all, because it's returning before any of the work is complete. You need to terminate your function only after all the asynchronous work is complete. The only way you'd know that is by tracking the promises. firebase.google.com/docs/functions/terminate-functionsDoug Stevenson

1 Answers

1
votes

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;
});