I have tried to look through all the similar questions but none of them fully answers my problem.
I am trying to use rottentomatoes api to retrieve a list of movies corresponding to a general title search and display for each of them their directors.
Now, the directors must be fetched through a separate json (the one corresponding to the film itself) therefore I have to do a http.request
to obtain the list of movies and then, for each of them, make another http.request
to fetch the corresponding director.
The problem is when I try to loop inside the first res.on('end' , function(){..})
, so that for each movie retrieved I can do another http.request
.
I know that all the requests are asynchronous but this shouldn't be a problem, instead, the director of the first film gets attached to the json object containing the director of the same first film and when I try to parse this object I get an error
undefined:1
.rottentomatoes.com/api/public/v1.0/movies/770687943/similar.json"}}{"id":9272
^
Since two json responses get attached to themselves, they are not json anymore, thus the error.
It's as if the first iteration of the loop (the one to fetch the directors) gets executed twice. It shouldn't be a problem of favicon etc., I think.
I have found by trial and error that THIS way of looping works:
function httpGetMovies(response) {
var fullRequestQueryMovie = partialQueryMovie + apiKeyMovie + '&' + queryTermMovie + '&page_limit=2'+'&_prettyprint=true';
console.log('Calling -> ' + fullRequestQueryMovie);
var headersMovie = {
'Content-Type': 'application/json'
};
var optionsMovie = {
host: hostMovie,
path: fullRequestQueryMovie,
method: 'GET',
headers: headersMovie
};
var jsonStringResponseMovie = '';
var req = http.request(optionsMovie, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(piece) {
jsonStringResponseMovie += piece;
//console.log("-----------------\n"+jsonStringResponseMovie);
});
var i=0;
res.on('end', function addDirector() {
var jsonObj = JSON.parse(jsonStringResponseMovie);
var moviesBag = [];
console.log("film n°: "+i+" of "+jsonObj.movies.length+" "+jsonObj.movies[i].title);
// /-----begin nested http.request--------
var singleMovieDirectors = [];
var fullRequestSingleMovie = jsonObj.movies[i].links.self + '?apikey=' + apiKeyMovie ;
var headersSingleMovie = {
'Content-Type': 'application/json'
};
var optionsSingleMovie = {
host: hostMovie,
path: fullRequestSingleMovie,
method: 'GET',
headers: headersSingleMovie
};
var jsonStringResponseSingleMovie = '';
var req2 = http.request(optionsSingleMovie, function(res2) {
console.log("request for "+optionsSingleMovie.path);
// console.log("statusCode: ", res.statusCode);
// console.log("headers: ", res.headers);
res2.on('data', function(piece2) {
jsonStringResponseSingleMovie += piece2;
});
res2.on('end', function() {
var jsonObjSingleMovie = JSON.parse(jsonStringResponseSingleMovie);
console.log("directors > "+JSON.stringify(jsonObjSingleMovie.abridged_directors));
// response.send(blabla);
if (i++ < jsonObj.movies.length-1){
addDirector()
}
else{
console.log ("finished adding directors");
}
});
});
req2.end();
req2.on('error', function(e) {
console.error(e);
});
// -------end of nested http.request-----------------
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
};
You can see that there is a i=0
before the res.on('end')
of the main http.request
, and inside there is the nested request with a check and and increment of i at its end, if there are any movies left, I'll call again the callback function but with i++
, thus shifting to the next movie.
Note that this call #movies
times the callback function
THIS WAY, instead, DOESN'T WORK and I DON'T KNOW WHY
function httpGetMovies(response) {
var fullRequestQueryMovie = partialQueryMovie + apiKeyMovie + '&' + queryTermMovie + '&page_limit=2'+'&_prettyprint=true';
console.log('Calling -> ' + fullRequestQueryMovie);
var headersMovie = {
'Content-Type': 'application/json'
};
var optionsMovie = {
host: hostMovie,
path: fullRequestQueryMovie,
method: 'GET',
headers: headersMovie
};
var jsonStringResponseMovie = '';
var req = http.request(optionsMovie, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(piece) {
jsonStringResponseMovie += piece;
//console.log("-----------------\n"+jsonStringResponseMovie);
});
res.on('end', function () {
var jsonObj = JSON.parse(jsonStringResponseMovie);
var moviesBag = [];
for(i = 0; i<sonObj.movies.length-1; i++){
console.log("film n°: "+i+" of "+jsonObj.movies.length+" "+jsonObj.movies[i].title);
// /-----begin nested http.request--------
var singleMovieDirectors = [];
var fullRequestSingleMovie = jsonObj.movies[i].links.self + '?apikey=' + apiKeyMovie ;
var headersSingleMovie = {
'Content-Type': 'application/json'
};
var optionsSingleMovie = {
host: hostMovie,
path: fullRequestSingleMovie,
method: 'GET',
headers: headersSingleMovie
};
var jsonStringResponseSingleMovie = '';
var req2 = http.request(optionsSingleMovie, function(res2) {
console.log("request for "+optionsSingleMovie.path);
// console.log("statusCode: ", res.statusCode);
// console.log("headers: ", res.headers);
res2.on('data', function(piece2) {
jsonStringResponseSingleMovie += piece2;
});
res2.on('end', function() {
var jsonObjSingleMovie = JSON.parse(jsonStringResponseSingleMovie);
console.log("directors > "+JSON.stringify(jsonObjSingleMovie.abridged_directors));
// response.send(blabla);
});
});
req2.end();
req2.on('error', function(e) {
console.error(e);
});
// -------end of nested http.request-----------------
} // end of for loop
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
};
What's the problem, you may ask, if you have already found out the solution? First, I want to share, because it was suggested by a friend of mine and iIhonestly would have never thought of looping on the callback function itself. Second, I don't know why my option doesn't work. Where is the problem?
sorry for the long post, i tried to explain everything. and if it's a repost, feel free to point me out the way to heaven.
Max.