If I want to display all resources on a page, I might do:
Resource.find({}).exec(function (err, resources) {
res.render("view", {
resources: resources
However, what if I want to display all resources and all projects on the page simultaneously? I could do:
Resource.find({}).exec(function (err, resources) {
Projects.find({}).exec(function (err, projects) {
res.render("view", {
resources: resources,
projects: projects
I think that there has to be a better/more correct way to do this, though.
var async = require('async');
var resourcesQuery = Resource.find({});
var projectsQuery = Projects.find({});
var resources = {
resources: resourcesQuery.exec.bind(resourcesQuery),
projects: projectsQuery.exec.bind(projectsQuery)
};
async.parallel(resources, function (error, results) {
if (error) {
res.status(500).send(error);
return;
}
res.render("view", results);
});
This will make the queries in parallel instead of serial, which will probably be faster.