Skip to content

Instantly share code, notes, and snippets.

@cookie-ag
Created September 12, 2016 03:26
Show Gist options
  • Save cookie-ag/e586acecf5c8e3c90f1402f538ccb8c3 to your computer and use it in GitHub Desktop.
Save cookie-ag/e586acecf5c8e3c90f1402f538ccb8c3 to your computer and use it in GitHub Desktop.
Simple use of async parallel to find user and count them
/*-------------- Simple use of async parallel to find user and count them --------------*/
var NoticiaModel = require('../models/noticias');
var User = require('../models/user');
var ObjectId = require('mongoose').Types.ObjectId;
var async = require('async');
exports.index = function(req, res) {
async.parallel([
/*-------------- find, n=1 --------------*/
function(callback) {
NoticiaModel
.find()
.sort({ data: 'desc' })
.populate('_criador')
.exec(function(err, noticias) {
callback(err, noticias);
});
}
],
/*-------------- results from all async callbacks in results[n] --------------*/
function(err, results) {
if (err) {
return res.send(err);
}
User.count({}, function(err, TotalUsers) {
res.render('admin/admin', {
user: req.user,
noticias: results[0], // get the user out of session and pass to template
title: "Seja bem vindo ao ",
subtitle: "Visualize todas as sua notícias",
TotalUsers: TotalUsers
})
})
})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment