【发布时间】:2018-05-16 11:15:44
【问题描述】:
我正在做一个学生项目。我的目标是收集数据并存储它。 我将 nodeJs 与 express 一起使用,然后在 ThemoviesDb 上使用 superagent 运行查询以收集电影,然后将它们存储在 neo4j 上。
这是我的例子:
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 );
});
我只是在执行一个循环( 39 因为 api 的限制是 40 ) 对于每一个我都会提出要看电影的请求。
如您所见,我首先显示所有 id,然后显示与 id 匹配的标题。 --> 我想等到请求结束后再继续。 我看起来答应了,但我没有一切。
然后我认为我在 id / film 上的问题是由于这个。
感谢您的帮助,对不起我的英语
【问题讨论】:
-
变量film 和Filmadded 无处可寻,cpt 也是如此。你能发布完整的代码或指向一个 git repo 吗?
标签: javascript node.js asynchronous promise request