如何将各种猫鼬结果连接到一个JSON数组以显示在DataTable上? [英] how to concatenate various mongoose result to one JSON Array to display on DataTable?

查看:85
本文介绍了如何将各种猫鼬结果连接到一个JSON数组以显示在DataTable上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将不同的猫鼬查询结果连接到一个数组. 我的猫鼬结果如branch_data

I need to concatenate different mongoose query results to one array. i have a mongoose result like branch_data

[ { _id: 59a270e53abb8426805b97fb,
client_name: 'Client 1',
branch_name: 'Branch 1',
router_serial: '111111111',
data_card_serial: '11111111',
sim_number: '11111111111',
modem_serial: '11111111111',
idu_serial: '1111111111',
dispatch_date: '08/27/2017',
status: 'installed',
action: 'primary();',
__v: 0,
ir_report: '201708271241491111111111.xlsx',
notes: '111111111111111111',
ip_address: '1111111111111',
installation_date: '08/01/2017' },
{ _id: 59a274045f867701fc07792e,
client_name: 'Client 2',
branch_name: 'Branch 2',
router_serial: '2222222222222',
data_card_serial: '22222222',
sim_number: '2222222222222',
modem_serial: null,
idu_serial: null,
dispatch_date: '08/02/2017',
status: 'installed',
action: 'primary();',
__v: 0,
ir_report: '2017082712552322222222222.xlsx',
notes: '22222222222222222',
ip_address: '22222222222',
installation_date: '08/02/2017' },
{ _id: 59a277ae27e9d40020f373ae,
client_name: 'Client 3',
branch_name: 'Branch 3',
router_serial: '333333333333',
data_card_serial: '3333333333',
sim_number: '3333333333',
modem_serial: '3333333333333333',
idu_serial: '3333333333333',
dispatch_date: '08/03/2017',
status: 'installed',
action: 'primary();',
__v: 0,
ir_report: '2017082713103733333333333.xlsx',
notes: '333333333333333',
ip_address: '333333333333',
installation_date: '08/03/2017' } ]

在这里我遍历此结果并将其保存到不同的数组中,

here i iterate through this result and save it to different arrays,

Dispatched.find({status:"installed"},function(err,dispatched_data) {
    //console.log("SUCCES data: "+dispatched_data);
    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);
            modem_serial.push(dispatched_data[j].modem_serial);
            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});
        }

然后将这些数组传递给另一个猫鼬findOne查询以获取结果;

and then i pass these array to another mongoose findOne query to get result;

async.mapSeries(branch_name, function (item, done) {
        Branch.findOne({ b_code: item }, { _id: 0, __v:0 }, function (err, data) {
            // if an error occurs, stop everything
            if (err)
                return done(err);
            // if a modem is found, send it back
            if (data)
                return done(null, data);
            // otherwise
            done(null, {  r_name: 'No data',r_serial_no: 'No data' });
        });
    }, function (err, data) {
        // when the iteration is done or if an error occurred, it will come here
        console.log("\n\n\n Branch=> ", data);
        concatData(data)
    });

    async.mapSeries(serial, function (r_serial_no, done) {
        Router.findOne({ r_serial_no: r_serial_no }, { _id: 0, __v:0 }, function (err, r_data) {
            // if an error occurs, stop everything
            if (err)
                return done(err);
            // if a modem is found, send it back
            if (r_data)
                return done(null, r_data);
            // otherwise
            done(null, {  r_name: 'No data',r_serial_no: 'No data' });
        });
    }, function (err, routers) {
        // when the iteration is done or if an error occurred, it will come here
        console.log("\n\n\n Router=> ", routers);
        concatData(routers);
    }); 
....
....

现在我得到了所有结果,但是我无法将其连接起来.请帮助

and now i got all the results but i can't concatenate it. please help

ie; finalJSON =数据+路由器+等等.

ie; finalJSON = data+routers + etc..

推荐答案

您可以使用 async.series()来运行每个任务.每个任务,例如getBranches()getSerials()将返回"数据数组.完成该系列后,您应该具有一个数据数组,因此您需要对其进行展平.

You can use async.series() to run each task. Each task e.g. getBranches() and getSerials() will "return" an array of data. When the series is done, you should then have an array of array of data, so you need to flatten it.

async.series([
    function getBranches(done) {
        async.mapSeries(branch_name, function (item, done) {
            // FYI 'done' inside this function is not the same 'done' as outside the function
            // ...
        }, done);
    },
    function getSerials(done) {
        async.mapSeries(serial, function (r_serial_no, done) {
            // ...
        }, done);
    },
    // etc
], function (err, data) {
    // data should come back as multidimensional array
    // so you should only need to flatten it
    var finalJSON = [].concat.apply([], data);
});

有关扁平化JavaScript中的数组数组,请参见答案.

See this answer regarding flattening an array of arrays in JavaScript.

编辑:我从未使用过 async.concatSeries ()之前,但可能要短一些.

Edit: I've never used async.concatSeries() before but it might be shorter.

这篇关于如何将各种猫鼬结果连接到一个JSON数组以显示在DataTable上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆