javascript - NodeJS Express: How can I get my object to build completely before sending it in the response? -


i trying build result_arr of location objects send response, not sure how send response when entire array has been built. response contains empty array, result_arr array filled after response has been sent.

function handle_getlocations(req, res, done){ var con_id = req.body["contractor_id"]; console.log("contractor id :" + con_id.tostring()); var result_arr = new array();  employee.getactivebycontractor(con_id, function(err, employees){     if (err) {         console.log("logging error in json:\n");         res.json({"code" : 100, "status" : "error in connection database"});         return;     };      if(employees.length === 0) done(null);      for(var i=0;i<employees.length;i++){         assignment.getlocationsbyemployeeid(employees[i].employee_id, function(err, locations){             if (err) {                 console.log("logging error in json:\n");                 res.json({"code" : 100, "status" : "error in connection database"});                 return;             };             console.log("number of locations: " + locations.length.tostring());             for(var j=0;j<locations.length;j++){                 console.log("assignment is: " + locations[j].assignment_id.tostring());                 location.getallbyid(locations[j].location_id, function(err, loc){                     if (err) {                         console.log("logging error in json:\n");                         res.json({"code" : 100, "status" : "error in connection database"});                         return;                     };                      var loc_obj = {};                     loc_obj.display_name = loc[0].display_name;                     loc_obj.location_id = loc[0].location_id;                     console.log("location is: " + loc_obj.display_name);                     console.log("location id is: " + loc_obj.location_id.tostring());                      result_arr.push(loc_obj);                     console.log(result_arr);                     done(result_arr);                 });             };         });     }; }); 

};

i know in nodejs idea not make blocking calls, not sure how make sure of information sent in response.

you calling many asynchronous functions in loop , not have logic check when completed send response client.

i modified code bit add logic in vannilajs way messy below working code.

anyways suggest use promise based/asynchronous modules async, bluebird etc handle nicely. using them, can improve readability , easy maintainability in code rid of callback hells , other disadvantages.

async http://caolan.github.io/async/

bluebird https://github.com/petkaantonov/bluebird

you can read more on below link,

https://strongloop.com/strongblog/node-js-callback-hell-promises-generators/

function handle_getlocations(req, res, done){    var con_id = req.body["contractor_id"];    console.log("contractor id :" + con_id.tostring());    var result_arr = new array();     employee.getactivebycontractor(con_id, function(err, employees){     if (err) {         console.log("logging error in json:\n");         res.json({"code" : 100, "status" : "error in connection database"});         return;     };      if(employees.length === 0) done(null);      var employeeschecked = 0;     var errors = [];      function sendresponse(){        if(employeeschecked === employees.length) {        res.json(result_arr);        //done(result_arr);  // if required, uncomment line , comment above line       }     }      for(var i=0;i<employees.length;i++){         assignment.getlocationsbyemployeeid(employees[i].employee_id, function(err, locations){             var locationschecked = 0;             if (err) {                 console.log(err);                 errors.push(err);                 ++employeeschecked;                  sendresponse();             } else {             console.log("number of locations: " + locations.length.tostring());             for(var j=0;j<locations.length;j++){                 console.log("assignment is: " + locations[j].assignment_id.tostring());                 location.getallbyid(locations[j].location_id, function(err, loc){                     ++locationschecked;                     if (err) {                         console.log(err);                         errors.push(err);                     } else {                     var loc_obj = {};                     loc_obj.display_name = loc[0].display_name;                     loc_obj.location_id = loc[0].location_id;                     console.log("location is: " + loc_obj.display_name);                     console.log("location id is: " + loc_obj.location_id.tostring());                      result_arr.push(loc_obj);                     console.log(result_arr);                    }                     if(locationschecked === locations.length) {                       ++employeeschecked;                    }                     sendresponse();                 });             }           }         });     } });  } 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -