1

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).

3
  • what do you expect results.one and results.two will be? Neither of the functions returns anything either, so, how do you expect the results to be available in function(err, results) ? Commented Feb 28, 2018 at 2:54
  • change one: function(){ to one: function(callback){ ... and instead of function(err, recs){ .... } replace that with callback ... repeat for two Commented Feb 28, 2018 at 3:01
  • ohhh i see. well just adding 'callback' like how h1b9b and Jaromanda X said did the trick, thanks a lot Commented Feb 28, 2018 at 3:04

2 Answers 2

1

The tasks have the callback function as argument
So the return data from a task, it should look like

task: function(callback) {
   ...
   callback(null, result);
}

In this case, you can use the callback as the second argument to Country.aggregate

Sign up to request clarification or add additional context in comments.

1 Comment

or, in the case of the code in the question, instead of using function(err, recs) { .... } just use callback
0

Parllel require a callback function which make sure that function is fully executed and now its time to return.

Even callback take 2 parameters , 1. Err (which will null if success) 2. Data(which need to send in success)

var async = require('async');
    async.parallel({
    one: function(cb){
        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){
               cb(err);
            } else {
               cb (null , recs);
            }
        });
    },

    two: function(cb){
        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){

                cb(err);
            } else {
               cb(null, recs);
            }
        });
    }
}, function(err, results){
    res.render('random', { title: 'Test', data: results.one, pie: results.two });
});

You can read further details on https://github.com/caolan/async/blob/v1.5.2/README.md#parallel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.