Reputation: 336
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);
}
});
}
Upvotes: 1
Views: 2425
Reputation: 20288
Try this:
PersoModel
.where('name').in(group.contacts)
.exec(function(person){
console.log('Found ' + person._id + ' ' + person.name);
});
Upvotes: 2