I am trying to use the async module to cut back the 'callback hell' specific to Node.js. Basically, I am trying to use async.series
to retrieve some info from the database and display it in my view. Still, I get no results in my view.
This is the code that I have so far:
// Search
exports.search = function(req, res) {
var x = [];
async.series([
function(cb) {
Lang.find({ lang: req.query.keyword }).sort({ verbal: -1 }).exec(function(err, langs) {
cb(null, langs);
});
},
function(cb) {
Human.find({}, function(err, humans) {
cb(null, humans);
});
}],
function(err, results) {
if (err) {
res.send(500);
}
for(var i = 0; i < results[0].length; i++) {
for(var j = 0; j < results[1].length; j++) {
if(results[1][j]._id == results[0][i].human) {
x.push(results[1][j]);
}
}
}
res.render('myView', { title: 'Search Results', humans: x });
}
);
}
I first want to query the Lang model (MongoDB) and find the records that match req.query.keyword
. Afterwards, I want to query the Human model and find all the Humans that have that specific language skill. Also, the FOR loop is meant to eliminate duplicates from my array since a Human might have several languages.