Hello I need to do multiple aggregate queries, and render the results to a view. My problem right now is figuring out how to make sure all the queries finish before rendering them and I have come upon async.parallel. I have been trying to mess with it but it is not working. For example, this is what I have right now.
var async = require('async');
async.parallel({
one: function(){
Country.aggregate([
{
$match: {
name: { $in: ["USA", "China", "India", "France", "Japan"]},
}
},
{
$sort: {
year: 1
}
},
{
$group: {
_id: '$name',
nominalGDP: {$push: "$nominalGDP"}
}
},
{
$project: {
_id: 0,
name: "$_id",
nominalGDP: 1
}
}
], function(err, recs){
if(err){
console.log(err);
} else {
console.log(recs);
}
});
},
two: function(){
Country.aggregate([
{
$match: {
$and: [
{name: { $in: ["USA", "China", "India", "France", "Japan"]}},
{year: 2017}
]
}
},
{
$sort: {
"nominalGDP": -1
}
},
{
$project: {
_id: 0,
name: 1,
'y' : '$nominalGDP'
}
}
], function(err, recs){
if(err){
console.log(err);
} else {
console.log(recs);
}
});
}
}, function(err, results){
res.render('random', { title: 'Test', data: results.one, pie: results.two });
});
So I have two aggregate functions that each produce their results. The two functions are printing their results properly, but no data is being rendered to my view (random).
results.oneandresults.twowill be? Neither of the functions returns anything either, so, how do you expect the results to be available infunction(err, results)?one: function(){toone: function(callback){... and instead offunction(err, recs){ .... }replace that withcallback... repeat fortwo