I am definitely not seeing this straight so am looking for a quick hand. I've been googling about and not found much that has helped.
Set the scene:
I have a node app that uses the 'googleapis' package. Now, I have that working fine and returning data (mostly) as expected.
The problem comes when I have to make a separate call to get the duration for each video (only way to get that bit of data is with a separate call).
I don't seem to be able to pass the duration value up the scope. I know I'm not doing it right and need to do it different but I just can't see the wood for the trees here.
youtubeClient.search.list({ part: 'id,snippet', maxResults: 10, q: searchTerm, type: 'video' },
function (err, data) {
if (err) {
console.error('Error: ' + err);
}
if (data) {
var returnList = [];
for (var item in data.items) {
var duration = '00:00'; // THIS IS THE VARIABLE I WANT POPULATED
var value = data.items[item].id.videoId;
youtubeClient.videos.list({ part: 'contentDetails', id: data.items[item].id.videoId },
function (err, details) {
if (err) {
console.error('Error: ' + err);
}
if (details) {
// THIS IS WHERE I AM TRYING TO SET THE DURATION FIELD FROM THE RESULT
duration = details.items[0].contentDetails.duration;
}
});
returnList.push({value: value, duration: duration});
}
res.json(returnList);
}
});
As you can see, what I am trying to achieve shouldn't be at all difficult so any help would be appreciated. Thank you.