node.js - Request within a request in nodejs -
i using request library in nodejs. need call new url within request not able join response asynchronouse. how send variable a in below request containing result of request within request.
request({ url: url, json: true }, function (error, response, body) { var = []; a.push(response); (i = 0; < a.length; i++) { if (a.somecondition === "rightcondition") { request({ url: url2, json: true }, function (error, response, body) { a.push(response); }); } } res.send(a); });
your code seems right want. sending response in wrong callback. move sends after second request has completed:
request({ url: url, json: true }, function (error, response, body) { var = []; a.push(response); request({ url: url2, json: true }, function (error, response, body) { for(i=0;i<a.length;i++){ if(a.somecondition === "rightcondition"){ a.push(response); } } res.send(a); // send after last request }); });
Comments
Post a Comment