Okay, there is quite a bit happening here but I tried to isolate the issue as much as possible.
In my node.js Express project, I'm making two API request calls that the second call is dependant on the first. To make this task easier, I'm using waterfall method from async module.
In getVideoDetails function, I put the second API request in loop with the videoId retreived from the first response to get video data.
The problem currently I have is that, var extended only gives me body value while I expect it to be result + body.
I wonder that's because _extend shouldn't be inside the loop.
I'm also not clear how I can make the result accessible to call callback(result) outside the loop.
async.waterfall([
function getVideos (getVideoCallback) {
...
},
function getVideoDetails (result, getVideoDetailsCallback) {
var urls = [];
Object.keys(result.items).forEach(function(item) {
urls.push ("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=" + result.items[item].contentDetails.videoId + "&key=xxx");
})
urls.forEach(function(url) {
request( url, function(err, response, body) {
if(err) { console.log(err); return; }
body = JSON.parse(body);
var extended = _.extend(result, body);
getVideoDetailsCallback(null, extended);
});
});
}
], function (err, result) {
if (err) { console.log(err); return; }
callback(result);
});