Reputation: 1266
I want to call two mongoose queries "Parallel" and pass the returned data of both the queries to client side.
//both queries should be called parallel, not one after another
//query 1
PaperModel.find().then((papers) => {
});
//query 2
ConferenceModel.find().then((conferences) => {
});
//this function should only be called when both the
//queries have returned the data
res.render('Home', {
Papers: papers
Conferences: conferences
});
I tried to look at this but didn't get it well. Thanks
Upvotes: 0
Views: 740
Reputation: 1565
If PaperModel.find() and ConferenceModel.find() returning promises you can use something like in code below:
//query 1
const papers = PaperModel.find();
//query 2
const conferences = ConferenceModel.find();
Promise.all([papers, conferences]).then((values) => {
res.render('Home', {
Papers: values[0]
Conferences: values[1]
});
})
and another option with wrapping function with async await syntax
const getData = async () => {
const papers = PaperModel.find();
const conferences = ConferenceModel.find();
const values = await Promise.all([papers, conferences]);
res.render('Home', {
Papers: values[0]
Conferences: values[1]
});
}
Upvotes: 1