In my custom module for dealing with data, I send 2 requests for first getting list of videos from my playlist and secondly,I send another request using videoID
that I got from the first result.
Because the 2nd request is dependant on the 1st one, I'm using waterfall
method of async module.
To check this is working correctly, I'm logging the result from the 1st request in the 2nd function but the web page is not running and returning nothing in the console.
Is there any problem here?
var data = function(callback) {
var request = require('request'),
async = require('async');
async.waterfall([
function getVideo (getVideoCallback) {
var url = "https://www.googleapis.com/youtube/v3/playlistItems";
var properties = {
part: 'snippet, contentDetails',
playlistId: 'xxx',
key: 'xxx',
maxResults: 50
}
request({ url:url, qs:properties}, function(err, response, body) {
if(err) { console.log(err); return; }
body = JSON.parse(body);
getVideo(body, getVideoCallback);
});
},
function getVideoDetails (result, getVideoDetailsCallback) {
console.log(result);
...
}
], function (err, result) {
if (err) { console.log(err); return; }
callback(result);
});
}
module.exports = data;
In your first waterfall function
request({ url:url, qs:properties}, function(err, response, body) {
if(err) { console.log(err); return; }
body = JSON.parse(body);
getVideo(body, getVideoCallback);
});
You call the first waterfall function again instead of calling the function callback, a recursive call with the wrong arguments. In order to progress to the next function in the waterfall stack, you'll need to call the callback like so:
request({ url:url, qs:properties}, function(err, response, body) {
if(err) { console.log(err); return; }
body = JSON.parse(body);
getVideoCallback(null, body);
});
Checkout async.js waterfall's example: