Async parallel to pass data in object

So I'm trying to pass data from two databases to my dashboard view after user gets logged in, however...

  1. Don't know if that's the right way of doing.
  2. Don't know how to access those two objects that are passed in results.

router.get("/dashboard", middleware.isLoggedIn, function (req, res) { async.parallel([ function (callback) { callback(null, User.findById(req.user)); }, function (callback) { callback(null, Instrument.find({})); } ], function (err, results) { res.send("all good" + results); console.log(results) }); });

1 answer

  • answered 2018-04-14 16:28 Zeeshan Hassan Memon

    Try it like this:

    router.get("/dashboard", middleware.isLoggedIn, function (req, res) {
        async.parallel([User.findById(req.user), Instrument.find({})
            ],
            function (err, results) {
                if (err) {
                    // handle error here
                } else {
                    // results[0] will have user object
                    // results[1] will have instrument object
                    res.send(results); // sending json to client
                }
            });
    });