0

i need to concatenate mongoose query result to single JSON Object. the problem is i am passing an array to mongoose ie;

Modem Serial: [11111111111,nodata,3333333333333333]

what i need is to concatenate data like this;

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"}]

this is my code;

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

                });
            }

but after refresh i am getting this,

Final 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??

here is my complete code,

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

1 Answers1

0

for loop is synchronous whereas findOne is asynchronous. The callback for findOne may not get fired in the order that findOne gets fired.

I would use async.js for this, particularly async#mapSeries because you seem to be trying to convert each element in one array into another array, and you also want to retain the order.

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);
});
Mikey
  • 6,728
  • 4
  • 22
  • 45
  • Thank you very much! it worked! i tried async earlier but i didnt get a correct result. i tried waterfall and serial in async. but i didn't understand that. –  Aug 27 '17 at 17:40
  • hello `Mickey`, i had a doubt i have multiple arrays like modem_serial and multiple results like `modems`. how to concatenate all the results to one json array. i want it for displaying it in data table. –  Aug 28 '17 at 15:23
  • @AnonymousObject Post another question including an example of what you start with and what you want to end with. – Mikey Aug 28 '17 at 15:25
  • bro if i post code then too many down votes will come because i have a junk of data.. –  Aug 28 '17 at 15:26
  • @AnonymousObject You're asking a new question hence why you should post a new question. You only get downvotes if you don't explain what you're trying to do well and have no code or too much code of what you tried. For example, everything up to **here is my complete code** in this question was enough -- everything else was too much. When you post junk data, keep it minimum as possible, post only a snippet of what you have and what you want. – Mikey Aug 28 '17 at 15:35
  • bro i posted question, https://stackoverflow.com/questions/45922652/how-to-concatenate-various-mongoose-result-to-one-json-array-to-display-on-datat –  Aug 28 '17 at 15:38