node.js - 异步系列nodejs中的async foreach

标签 node.js mongodb express node-async

我正在开发 Node 异步库。我无法按照我想要的顺序执行。我不知道我哪里错了 这是代码..在评论中我已经定义了订单号.. 目前它按 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});
    }); 
};

最佳答案

所有 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});
    }); 
}; 

关于node.js - 异步系列nodejs中的async foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36795582/

相关文章:

arrays - 如何在 Mongoose 中搜索填充数组 [包含 ref 的数组]

node.js - 使用 Where 和 Include 失败的 Sequelize 查询

javascript - 将长查询字符串直接解析到 MongoDB(就像在 SQL 中一样)

MongoDB 社区尚不支持 Ubuntu 22.04,有什么解决方法吗?

node.js - 获取 "first argument must be a string or Buffer"错误

http - Express.js HTTP 请求超时

node.js - 如何运行 promise

node.js - 按字母顺序对数据进行排序并使用 Expressjs 和 MongoDB 传递到 View

CSS 在部署中似乎与在开发中不同

javascript - 如何在 Javascript 中更改 JSON 值模式