Javascript 猫鼬&x2B;Node.js:异步问题

Javascript 猫鼬&x2B;Node.js:异步问题,javascript,node.js,mongodb,asynchronous,mongoose,Javascript,Node.js,Mongodb,Asynchronous,Mongoose,我的ReadyForReview收藏中有一堆个人资料。每个配置文件都包含一个“用户id到审查”字段。我想使用user_id_to_review将用户集合中的用户信息附加到每个配置文件中 // looking for all ReadyForReview profiles ReadyForReview.find() .exec(function(err, profilesReadyForReview) { var profilesReadyForReviewArray = [] //arr

我的ReadyForReview收藏中有一堆个人资料。每个配置文件都包含一个“用户id到审查”字段。我想使用user_id_to_review将用户集合中的用户信息附加到每个配置文件中

// 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
    }

})
//查找所有ReadyForReview配置文件
ReadyForReview.find()文件
.exec(函数(err、profilesReadyForReview){
var profilesReadyForReviewArray=[]//将填充配置文件和用户信息的数组
//对于每个配置文件,从用户表中找出用户的信息
对于(变量i=0;i

但是,由于异步,User.find函数中的i是错误的。如何获得一系列配置文件和用户信息?

使用异步库进行循环。


我认为你在这里是正确的,但是提到
档案repadyforreview[I]
可能只是
档案
起作用了!谢谢你们两位!愿密码之神在你的日子里光芒四射。
// 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
    async.each(profilesprofilesReadyForReview, function(profile, done) {
        var profileId = profile.user_id_to_review.toObjectId() // create objectID version of user_id_to_review
        User.find({
                '_id': profileId
            })
            .exec(function(err, user_whose_profile_it_is) {
                profilesReadyForReviewArray.push({
                    profile: profile,
                    user: user_whose_profile_it_is
                })
                done();
            });
        }, function(){
            console.dir(profilesReadyforReviewArray) // should be an array of profiles and user info
        });
});