What is the optimal way to send two independent MongoDB results in an Express application via HTTP Method?
Check out this concise example to clarify:
//app.js
var express = require('express');
var app = express();
var testController = require('./controllers/test');
app.get('/test', testController.getCounts);
...
The getCounts() function provided will not work because it attempts to send the response twice.
///controllers/test
exports.getCounts = function(req,res) {
Object1.count({},function(err,count){
res.send({count:count});
});
Object2.count({},function(err,count){
res.send({count:count});
});
};
Nevertheless, I am interested in consolidating those two counts into a single response object.
Should I trigger Object2.count within the callback of Object1 even though they are unrelated?
Alternatively, should I rethink the design in some other way?
Thanks!