【问题标题】:How to concatenate Mongoose query results inside a for loop?如何在 for 循环中连接 Mongoose 查询结果?
【发布时间】:2017-08-27 12:21:15
【问题描述】:

我需要将猫鼬查询结果连接到单个 JSON 对象。 问题是我将一个数组传递给 mongoose ie;

Modem Serial: [11111111111,nodata,3333333333333333]

我需要的是像这样连接数据;

Final Modem : [{"m_model":"Modem 1","m_serial_no":"11111111111","available":"dispatched"},
{"m_model":"No data","m_serial_no":"No data"},
{"m_model":"Modem3","m_serial_no":"3333333333333333","available":"dispatched"}]

这是我的代码;

for(i=0;i<modem_serial.length;i++){
        console.log("Modem Serial: "+modem_serial[i]);
        Modem.findOne({m_serial_no: modem_serial[i]},{_id: 0,__v:0},function (err,m_data) {
                    //console.log("err: "+err);
                    if(m_data!=null){
                        modem=modem.concat(m_data);
                    }else{
                        console.log("\n\n\nm_data : "+m_data);
                        modem=modem.concat({
                            m_model: 'No data',
                            m_serial_no: 'No data'
                        });
                    }
                    console.log("\n\n\nFinal Modem : "+JSON.stringify(modem));

                });
            }

但刷新后我得到了这个,

最终调制解调器:

 [{"m_model":"No data","m_serial_no":"No data"},{"m_model":"Modem 1","m_serial_no":"11111111111","available":"dispatched"},{"m_model":"Modem 3","m_serial_no":"3333333333333333","available":"dispatched"}]


the `No data` is going to first index. why??

这是我的完整代码,

router.get('/', function(req, res, next) {
Dispatched.find({status:"installed"},function(err,dispatched_data) {
    //console.log("SUCCES data: "+dispatched_data);
    var client_name=[],branch_name=[],serial=[],data_card_serial=[],sim_number=[],modem_serial=[],idu_serial=[],installed_date=[],
        ip_address=[],notes=[],ir_report=[],date,ipp={},ip=[],ir,note;
    for (var j = 0; j < dispatched_data.length; j++) {
            client_name=client_name.concat([{client_name:dispatched_data[j].client_name}]);
            branch_name.push(dispatched_data[j].branch_name);
            serial.push(dispatched_data[j].router_serial);
            data_card_serial.push(dispatched_data[j].data_card_serial);
            sim_number.push(dispatched_data[j].sim_number);
            if(dispatched_data[j].modem_serial != null){
                modem_serial.push(dispatched_data[j].modem_serial);
            }else{
                modem_serial.push("nodata");
            }

            idu_serial.push(dispatched_data[j].idu_serial);
            ip_address.push({ip_address:dispatched_data[j].ip_address});
            installed_date.push({installed_date:dispatched_data[j].installation_date});
            notes.push({notes:dispatched_data[j].notes});
            ir_report.push({ir_report:dispatched_data[j].ir_report});
        }
var data=[],router=[],datacard=[],sim=[],modem=[],idu=[],branch=[],i;
    console.log("Modem Serial: "+modem_serial.toString());
    console.log("\n\n\nDispatched Data Length ="+dispatched_data.length);
    for(i=0;i<modem_serial.length;i++){
        console.log("Modem Serial: "+modem_serial[i]);
        Modem.findOne({m_serial_no: modem_serial[i]},{_id: 0,__v:0},function (err,m_data) {
                    //console.log("err: "+err);
                    if(m_data!=null){
                        modem=modem.concat(m_data);
                    }else{
                        console.log("\n\n\nm_data : "+m_data);
                        modem=modem.concat({
                            m_model: 'No data',
                            m_serial_no: 'No data'
                        });
                    }
                    console.log("\n\n\nFinal Modem : "+JSON.stringify(modem));

                });
            }
res.end();
});
});

【问题讨论】:

    标签: json node.js mongodb mongoose


    【解决方案1】:

    for 循环是同步的,而findOne 是异步的。 findOne 的回调可能不会按照findOne 被触发的顺序被触发。

    我会为此使用async.js,尤其是async#mapSeries,因为您似乎正在尝试将一个数组中的每个元素转换为另一个数组,并且您还希望保留顺序。

    async.mapSeries(modem_serial, function (m_serial_no, done) {
        Modem.findOne({ m_serial_no: m_serial_no }, { _id: 0, __v:0 }, function (err, m_data) {
            // if an error occurs, stop everything
            if (err) 
                return done(err);
            // if a modem is found, send it back
            if (m_data) 
                return done(null, m_data);
            // otherwise
            done(null, { m_model: 'No data', m_serial_no: 'No data' });
        });
    }, function (err, modems) {
        // when the iteration is done or if an error occurred, it will come here
        console.log(err, modems);
    });
    

    【讨论】:

    • 非常感谢!有效!我之前尝试过异步,但没有得到正确的结果。我尝试了异步的瀑布和串行。但我不明白。
    • 你好Mickey,我怀疑我有多个像modem_serial这样的数组和像modems这样的多个结果。如何将所有结果连接到一个 json 数组。我希望它在数据表中显示。
    • @AnonymousObject 发布另一个问题,包括您从什么开始以及您想以什么结束的示例。
    • 兄弟,如果我发布代码,那么将会有太多的反对票,因为我有垃圾数据..
    • @AnonymousObject 您在问一个新问题,因此为什么要发布一个新问题。如果您没有解释您正在努力做的事情并且没有代码或您尝试过的代码过多,您只会得到否决。例如,在这个问题中,直到 here is my complete code 为止的所有内容都足够了——其他所有内容都太多了。当您发布垃圾数据时,请尽量减少垃圾数据,仅发布您拥有的和想要的内容的 sn-p。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多