async.waterfall随机排序结果

时间:2018-01-03 01:31:56

标签: node.js async.js

我正在编写一个嵌套代码,尝试使用async.waterfall或async.series,但每次刷新时我都会得到随机结果。这似乎是因为前两个函数的查询随机完成。

第一个查询正在排序到committed_date DESC。但是当我添加2个子查询时。排序变得歪曲。

Step1:循环登陆页面     Step1.1 - 获取details1存储库     Step1.2 - 获取details2版本 第2步:构建数组

db.collection('landingpages').find({is_deleted:{$ne:1}}).sort({committed_date:-1}).limit(10).toArray(function(err, db_results) { 
    var data_array = [];
    var x=1;

    if(db_results.length == 0) { 
        return_data.lps = data_array;
            parallel_done();
    }else{

        async.each(db_results, function (db_results1, cb) {

            async.watefall(
                [
                    function(callback) {
                        //if this is removed or passed as callback(null, ""); the sort is fixed from committed - 1
                        var data_repo = {};

                        db.collection('repositories').find({repository_id: repository_id}).toArray(function(err, db_results1) { 

                            if(db_results1.length == 0){
                                var data_repo = {};
                                callback(null, data_repo);
                            }else{
                                var data_repo = db_results1[0];
                                callback(null, data_repo);
                            }
                        });
                    },
                    function(callback) {
                        //if this is removed or passed as callback(null, ""); the sort is fixed from committed - 1
                        var data_version = {};

                        db.collection('versions').find({landingpage_id: landingpage_id}).sort({_id:-1}).limit(1).toArray(function(err, db_results1) { 
                            if(db_results1.length == 0){
                                var data_version = {};
                                callback(null, data_version);
                            }else{
                                var data_version = db_results1[0];
                                callback(null, data_version);
                            }
                        });
                    }
                ],
                function (err, data_repo,data_version) {

                    var document = {
                        "x": x++,   
                        "landingpage_id": db_results1.landingpage_id,
                        "repository_id": db_results1.repository_id,
                        "version_id": data_version.version_id,
                        "val": db_results1,
                        "data_repo": data_repo,
                        "data_version": data_version,
                    };

                    data_array.push(document);

                    if(data_array.length == db_results.length) { 
                        return_data.lps = data_array;
                    }
                }
            );
        });
    }

});

0 个答案:

没有答案