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

我需要连接不同的mongoose查询结果到一个数组。 我有一个像branch_data一样的mongoose结果

 [ { _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}); } 

然后我传递这些数组到另一个mongoosefindOne查询来获得结果;

 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 =数据+路由器+等。

       

网上收集的解决方案 "如何连接各种mongoose结果到一个JSON数组显示在DataTable上?"

您可以使用async.series()来运行每个任务。 每个任务,例如getBranches()getSerials()将“返回”一组数据。 当系列完成后,你应该有一个数组数组,所以你需要把它压扁。

 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中展开数组数组的问题。

编辑 :我从来没有使用async.concatSeries()之前,但可能会更短。