I have an Node.js function which queries multiple firebase collections. I want to iterate over a collection result set which will query another collection for each object returned.
async function getUserTestGraph(req, res) {
var userGraphId = req.query.id;
var userGraph = new Object();
var userTests = [];
snapshot = await db.collection('userGraphs').where('id', '==', userGraphId).get();
if (snapshot.empty) {
console.log('No matching user Graph found');
return res.status(200).json({graphStatus:200, message:"No user graph found!"});
} else {
userGraph['id'] = snapshot.docs[0].data().id;
userGraph['userId'] = snapshot.docs[0].data().userId;
userGraph['diseaseName'] = snapshot.docs[0].data().diseaseName;
userGraph['userDiseaseId'] = snapshot.docs[0].data().userDiseaseId;
// get user tests associated with graph
snapshotTests = await db.collection('userGraphTestSet').where('testSetId', '==', userGraphId).get();
testGraph = await snapshotTests.forEach( async function (test){
userTest = {};
userTest['id'] = test.data().id;
userTest['testSetId'] = test.data().testSetId;
userTest['userId'] = test.data().userId;
userTest['userTestId'] = test.data().userTestId;
userTest['testName'] = test.data().testName;
// get user test attributes
snapshotTestInfo = await db.collection('userTests').where('id', '==', test.data().userTestId).get();
if (!snapshotTestInfo.empty) {
userTest['testAmount'] = snapshotTestInfo.docs[0].data().testAmount;
userTest['testUnit'] = snapshotTestInfo.docs[0].data().testUnitId;
userTest['testDate'] = snapshotTestInfo.docs[0].data().testDate;
}
console.log("USER TEST ADDED FOR GRAPH 1");
console.log(userTest);
userTests.push(userTest);
console.log("USER TEST ADDED FOR GRAPH 2");
console.log(userTests);
});
console.log("USER TEST ADDED FOR GRAPH 3");
console.log(userTests);
return res.status(200).json({graphStatus:200, userGraph:userGraph, userTests: userTests});
}
}
For each graph there is a collection of tests. Querying the userGraphTestSet collection gets the list of tests for a particular graph. For each test in the collection I want to get test details from the UserTest collection. However using the await terminology for the testGraph variable to await for each graph test to get more test details from UserTest. However using a await on a forEach function doesn't seem to wait until it iterates over all graph tests. Instead when I curl the function I get the console.logs out of order. I get "USER TEST ADDED FOR GRAPH 3" first and "USER TEST ADDED FOR GRAPH 2" last. Does anybody know how to make the forEach functionality truly await so that it runs its coarse and hit the USER TEST ADD FOR GRAPH 3 last.
I changed the forEach loop to a for loop which itereated over the firebase snapshot result object.
for (var i = 0; i < snapshotTests.docs.length; i++) {
userTest = {};
userTest['id'] = snapshotTests.docs[i].data().id;
userTest['testSetId'] = snapshotTests.docs[i].data().testSetId;
userTest['userId'] = snapshotTests.docs[i].data().userId;
userTest['userTestId'] = snapshotTests.docs[i].data().userTestId;
userTest['testName'] = snapshotTests.docs[i].data().testName;
}