如何在Google Cloud功能(Firebase)的快照中获取子快照?

时间:2019-01-04 15:07:53

标签: javascript firebase firebase-realtime-database google-cloud-functions

每次删除 / users 节点时,我都试图在Google云函数中运行一些for Loops。

这是我正在使用的代码

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

admin.database()。ref('/ buildings /'+ eachBuilding).child(“ hasDepts”)的快照返回null。

我怎么去呢?除了 admin.database()。ref()外,我还尝试使用 firebase.database()。ref()达到目标,这是我用来执行的命令/对象使它在前端功能上运行。我也尝试了 functions.database(),但没有结果。

1 个答案:

答案 0 :(得分:0)

考虑道格·史蒂文森在第二次评论中提到的内容:

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