1

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

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 = data+routers + etc..

1 Answers1

1

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

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

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

Mikey
  • 6,728
  • 4
  • 22
  • 45
  • @Mickey, first of all Thank you for your valuable time & i have one another problem i need to add the final json array like the FinalJSONArray[1] = all the first result of getBranches,getSerials etc.. now all the data added to a single JSON Object. i need a array coz data table must need a array to iterate –  Aug 28 '17 at 16:10
  • 1
    @Mickey, Finally made it..! var sdata=JSON.stringify(data); var par=JSON.parse(sdata); var first=[]; for(var j=0;j –  Aug 28 '17 at 18:09