1

I am trying to run two async queries in MongoDB using Mongoose and ExpressJS.

exports.get_options_data = function (req, res) {
    var rooms = [];
    var areas = [];


    async.parallel({
        rooms : AddProperty.distinct("roomQty", function (err, data) {
            if (err)
                res.send({ code: '500', message: err });
            rooms = data;
        }),
        areas: AddProperty.distinct("area", function (err, data) {
            if (err)
                res.send({ code: '500', message: err });
            areas = data;
        })
    }, function(err, data){
        res.send({ code: '200', rooms: rooms, areas : areas });
    })
}

for that, I am using async parallel. I've install async using npm i async.

What I want is, to execute both these queries and send responses of queries together as combined JSON.

but, I am getting error when I execute this:

TypeError: wrapAsync(...) is not a function

Also, is there any better approach for doing same?

Ubiquitous Developers
  • 3,637
  • 6
  • 33
  • 78
  • 1
    `async.parellel` accepts function not query... `async.parallel({ rooms : function(callback) {...}, areas: function(callback) {...} }, function(err, data){ res.send({ code: '200', rooms: rooms, areas : areas }); })` – Ashh Mar 26 '19 at 06:36
  • The real question is why would you not just use `Promise.all()`. Unless you are bound to a very ancient version ( relatively speaking ) of nodejs then it's a "built in" way to do things. Also there is no point in the "outer arrays" you are keeping since you return `data.rooms` and `data.areas` in the outer callback already. That is when you actually use the correct syntax for `async.parallel` that is. Since presently you are not. – Neil Lunn Mar 26 '19 at 06:53
  • 1
    BTW The corrections are basically `rooms: (callback) => AddProperty.distinct("roomQty",callback)` and the same for your other `distinct `method. The basic point being the `function` with the argument `callback` which is itself the "callback function" to be sent to the final result block of the statement. – Neil Lunn Mar 26 '19 at 06:55
  • @AnthonyWinzlet, I am not very familiar about mongo and express. I am searching proper approach. so, I don't know is there any traditional approach is available in mongo or not – Ubiquitous Developers Mar 26 '19 at 06:56
  • Also, I read somewhere, I can use aggregate for this. I don't know whether it is correct or not? – Ubiquitous Developers Mar 26 '19 at 06:59
  • Mongoose is just an ORM. So basically approach/syntax is not related to the mongo here but with the nodejs. You must read this [link](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) for the syntax for the latest asynchronous(callbacks, async await, .then) calls. Or simply follow the above comments of the Neil Lunn. – Ashh Mar 26 '19 at 07:06
  • @AnthonyWinzlet - Ok let me check the document. – Ubiquitous Developers Mar 26 '19 at 07:09

2 Answers2

1

async. parallel accepts an array of functions rather than an object.

Promises are now supported in node so you can just use Promise.all as a replacement. You can turn AddProperty.distinct into a promise based function using util.promisify

const util = require('util');
const addProperty = util.promisify(AddProperty.distinct);


Promise.all([
    addProperty('roomQty'),
    addProperty('area')
]).then((data){
    res.send({ code: '200', rooms: data[0], areas : data[1] });
}).catch(error => {
   res.send({ code: '500', message: error });
});
Joey Ciechanowicz
  • 3,345
  • 3
  • 24
  • 48
0

Thanks Anthony Winzlet and Neil Lunn for your advice. As per your comments and guidance, I looked more into async.parallel and solve by below code:

exports.get_options_data = function (req, res) {
    async.parallel({
        rooms : function (callback) {
            AddProperty.distinct("roomQty", function (err, response) {
                callback(err, response)
            })
        },
        areas : function (callback) {
            AddProperty.distinct("area", function (err, response) {
                callback(err, response)
            })
        }
    }, function (err, data) {
        if (err)
            res.send({ code: '500', message: err });
        res.send({ code: '200', data: data });
    })
}
Ubiquitous Developers
  • 3,637
  • 6
  • 33
  • 78