异步系列nodejs中的async foreach

时间:2016-04-22 13:47:36

标签: node.js mongodb express node-async

我正在处理节点异步库。我无法执行我想要的序列。我不知道自己哪里出错了 这里是代码..在评论我定义了订单号.. 目前以 2,3,4,5,1 的顺序执行 1,2,3,4,5 顺序....请帮助

function getAsExhibitors(req, res) {
    //getting all exhibitors against an event
    var exhibitors = [];
    var eac_app_names = [];
    async.series([function(callback){
        models.EacExhibitorsExt.find({ deleted: false,userid: req.user._id}).sort({ modified: -1 }).exec(function(err, myExhibitors) {
            exhibitors = myExhibitors;
            callback();
        });
    },function(callback){
        async.forEach(exhibitors,function(exhibitor,callback){
            models.Eac.findById(exhibitor.eventid).exec(function(err,eac){
                eac_app_names[exhibitors.indexOf(exhibitor)]=eac;
                console.log("-----------------1--------------"+eac_app_names);
            });
            console.log("-----------------2--------------"+eac_app_names);
            callback();
        },function(err) {
            console.log("-----------------3--------------"+eac_app_names);
            callback();
        });
    }],function(err) { //This function gets called after the two tasks have called their "task callbacks"
        if (err) return next(err);
        //Here locals will be populated with 'exhibitors' and 'apps'
        console.log("-------------------------4------"+eac_app_names);
        console.log("-------------------------5------"+eac_app_names.name);
        res.locals.exhibitors = exhibitors; 
        res.locals.eac_app_names = eac_app_names;   
        res.render('eac/eac_reg_as_exhibitor', { title: "My Event Exhibitors", asexhibitor: exhibitors,app_names:eac_app_names});
    }); 
};

2 个答案:

答案 0 :(得分:1)

所有mongoose方法都是异步的。在你的场景中尝试这种方式:

function getAsExhibitors(req, res) {
    //getting all exhibitors against an event
    var exhibitors = [];
    var eac_app_names = [];
    async.series([function(callback){
        models.EacExhibitorsExt.find({ deleted: false,userid: req.user._id}).sort({ modified: -1 }).exec(function(err, myExhibitors) {
            exhibitors = myExhibitors;
            callback();
        });
    },function(callback){
        async.forEach(exhibitors,function(exhibitor,callback){
            models.Eac.findById(exhibitor.eventid).exec(function(err,eac){
                eac_app_names[exhibitors.indexOf(exhibitor)]=eac;
                console.log("-----------------1--------------"+eac_app_names);
 console.log("-----------------2--------------"+eac_app_names);
            callback();
            });

        },function(err) {
            console.log("-----------------3--------------"+eac_app_names);
            callback();
        });
    }],function(err) { //This function gets called after the two tasks have called their "task callbacks"
        if (err) return next(err);
        //Here locals will be populated with 'exhibitors' and 'apps'
        console.log("-------------------------4------"+eac_app_names);
        console.log("-------------------------5------"+eac_app_names.name);
        res.locals.exhibitors = exhibitors; 
        res.locals.eac_app_names = eac_app_names;   
        res.render('eac/eac_reg_as_exhibitor', { title: "My Event Exhibitors", asexhibitor: exhibitors,app_names:eac_app_names});
    }); 
}; 

答案 1 :(得分:-1)

欢迎您使用es6和发电机。 尝试co-foreach-series获取每个数组元素并逐个执行异步函数。

ForEach系列示例

foreach(yourArray, function(element, index) {
// Each of this function will be executed one after one
co(function*() {
   // Do some async task, and wait until this task be finished
   yield yourAsyncFunc(); 
   yield doOtherAsyncTasks();
})

})