I have a bunch of profiles in my ReadyForReview collection. Each profile contains a 'user_id_to_review' field. I want to use user_id_to_review to append the user's information from the Users collection to each profile.
// looking for all ReadyForReview profiles
ReadyForReview.find()
.exec(function(err, profilesReadyForReview) {
var profilesReadyForReviewArray = [] //array that will be populated with profiles and user info
// for each profile, figure out what the info for the user is from user table
for (var i = 0; i < profilesReadyForReview.length; i++) {
var thisUserProfile = profilesReadyForReview[i].user_id_to_review.toObjectId() // create objectID version of user_id_to_review
User.find({
'_id': thisUserProfile
})
.exec(function(err, user_whose_profile_it_is) {
profilesReadyForReviewArray.push({
profile: profilesReadyForReview[i],
user: user_whose_profile_it_is
})
})
console.dir(profilesReadyforReviewArray) // should be an array of profiles and user info
}
})
However, i in the User.find function is wrong due to async. How can I achieve an array of profiles and user information?