I’m trying to develop a simple application with mongoose.
Given an array of username ‘group.contacts’, I want to get the corresponding IDs of these user. However, the loop does not work as expected: the loop continues before the .find() method finish so I cannot get all the IDs of people. How can I solve this problem?
for(var v = 0; v < group.contacts.length; v++) {
PersonModel.find({
name: group.contacts[v]
}, function (err, person) {
if(!err && person) {
console.log('Found ' + person._id + ' ' + v);
}
});
}
Try this: