I am working on a student project. My goal is to collect data and store it. I use nodeJs with express, then I run queries with superagent on ThemoviesDb to collect movies and then store them on neo4j.
Here is my example :
app.get('/', function(req,res){
var hostname = "api.themoviedb.org/3";
var path = "/movie/";
// random id, to store random film
for(i=0;i<39;i++){
randomId = Math.floor(Math.random() * 500) + 80;
console.log(' --------------------- Random id is ---------------------- ' + randomId);
superagent.get(hostname + path + randomId)
.query({ api_key: api_key, append_to_response : 'credits' })
.end((err, res) => {
if (err) {
if(err.status_code = '34') {
return console.log('id : ' + randomId + 'ne correspond pas à un film')
}
else{
return console.log(err);
}
}
cpt ++;
title = res.body.original_title;
if(title){ // Test if title isn't nul, undefined, Nan, empty or 0 to add it
console.log('randomId --> ' + res.body.id + '--> ' + title );
if(!Filmadded.includes(film)){
Filmadded.push(film);
}
}
});
}
console.log('cpt : ' + cpt + '/39');
res.send('it works' + i );
});
I I'm just executing a loop ( 39 because the limit of the api is 40 ) and for each I make a request to get a movie.
As you can see i first have all the id's displayed then the titles that match the id. --> I would like to wait until the request is over to move on. I looked promised but I do not have everything.
I then think that my problem on id / film is due to this.
Thank you for your help and sorry for my english