I am having a trouble with mongoose used with node.js. In my model I am having only group supervisor's ID, and in this route I want also to add supervisorName to variable (this name is stored in Group model). So, yeah, I've read some about promises, but still, I dont have any idea how to solve that. (Basically, I just want to loop through all groups, get their models from mongodb, and assign supervisor name to every group)
router.get('/manage', function(req, res, next) {
Group.find({}, function(err, groups) {
groups.forEach(function(group) {
Group.findById(group.supervisor, function(err, supervisor) {
group.supervisorName = supervisor.name;
console.log(supervisor.name);
});
});
}).then(function() {
res.render('groups/groups_manage', {groups : groups});
});
});