如何将各种猫鼬结果连接到一个 JSON 数组以显示在 DataTable 上?

how to concatenate various mongoose result to one JSON Array to display on DataTable?

我需要将不同的猫鼬查询结果连接到一个数组中。 我有一个像 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' } ]

这里我遍历这个结果并将它保存到不同的数组,

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 查询以获得结果;

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);
    }); 
....
....

现在我得到了所有结果,但我无法将它们串联起来。请帮忙

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

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

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

请参阅此 answer 关于在 JavaScript 中展开数组的数组。

编辑:我以前从未使用过async.concatSeries(),但可能更短。