Tuesday 9 May 2017

node js:add mysql query result to json in forloop

I am trying to add query result from in for loop to JSON

function (req,res){
var result  = [{id:1
},{id:2},{id:3}];

for(var i=0;i<result.length;i++){
                        //query run
                       collection.getImages(result[i].id,function (status,error,image) {
                          //add query result to json
                          result[i]['images']=image;

                       });
                   }

 res.json(result);

}

But the final result doesn't contains the newly added key value(ie images) it because collection.getImages() is asynchronous so how can i
solve this?



via Jabaa

No comments:

Post a Comment