Joe Caraccio Joe Caraccio - 3 months ago 6
JSON Question

Is there a better way to query this data?

Just wanted to run this by some experts and get their thoughts on it. The way I am doing it just feels kinda hacky.

Here is my code:: Basically I find the Groups that the user is a Member of, then search for the group(s). As you can see I run a for loop that returns the object of each different group. That all works, but the problem is it seems like a lot of querying, and the callback function for some reason runs before the code is done executing.

var query = firebaseApp.database().ref().child('members').orderByChild('uid').equalTo(testUID);
query.once('value').then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
userObject = childSnapshot.val()
userGroups = userObject.groups.groupID

console.log("User Groups==")
console.log(userGroups)

pullGroupInfo(
function(){
console.log("###########@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
}
);
});
// end of get groups quer
});

function pullGroupInfo(callback) {
for(var i = 0; i < userGroups.length; i++) {
/* do some stuff */

var query = firebaseApp.database().ref().child('groups').orderByChild('id').equalTo(userGroups[i]);
query.once('value').then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
console.log("=")
console.log(childSnapshot.val())
groups.push(childSnapshot.val())
});
});
}
callback();
}


Here is my json in case it is needed:

{
"groups" : {
"-KO0C5FdTWisnc4i9xxM" : {
"id" : "jkdjsneind",
"members" : {
"General" : true,
"TechChat" : true
},
"name" : "joe",
"type" : "private"
},
"-KODDD0C5FdTWisnc4i9xxM" : {
"id" : "jkdjfbdkbfj",
"members" : {
"General" : true,
"TechChat" : true
},
"name" : "jack",
"type" : "private"
},
"-KODDD0C5FdTWisnc4i9xxMxx2" : {
"id" : "dfdsfds",
"members" : {
"General" : true,
"TechChat" : true
},
"name" : "bigcat",
"type" : "private"
},
"dicky" : "d",
"ggg" : "gg"
},
"members" : {
"-KO0C5FdTWisnc4i9xxM" : {
"ageRange" : "18-20",
"college" : "",
"dob" : "",
"firstname" : "joe",
"friends" : {
"UserID1" : true,
"UserID2" : true,
"UserID3" : true
},
"groups" : {
"groupID" : [ "jkdjfbdkbfj", "jkdjsneind" ]
},
"lastname" : "caraccio",
"name" : "joe",
"pendingFriends" : {
"UserID4" : true
},
"uid" : "6hOnKHBdkMWOaRFKiXyxLkU9lO33"

}

}


Any Help would be great!

Answer

Collect all the promises returned by query.once() into an array, then use Promise.all() to wait for all of them to resolve before calling callback.

function pullGroupInfo(callback) {
    var promises = userGroups.map(function(group) {
        for(var i = 0; i < userGroups.length; i++) {
            /* do some stuff */ 

            var query = firebaseApp.database().ref().child('groups').orderByChild('id').equalTo(group);  
            return query.once('value').then(function(snapshot) {
                snapshot.forEach(function(childSnapshot) {
                    console.log("=")
                    console.log(childSnapshot.val())
                    groups.push(childSnapshot.val())
                });  
            });
        }
    });
    Promise.all(promises, callback)
}
Comments