如何在node.js的防爆preSS async.each中使用要求 [英] How to use request within async.each in node.js Express
问题描述
在我的node.js的项目,我需要反复的API请求,所以我使用的方法async.each
控制台抱怨URL没有定义,我知道这一点,因为我只声明的URL是URL的数组。
我只是不知道如何从请求模块拼凑内async.each请求()。为了满足async.each()我已经放在网址作为第一个参数,但请求()本身就需要一个查询字符串参数,它是URL。
我还使用_.extend从下划线合并两个反应,但我不知道在那里我有它目前是在正确的地方。
可有人请点我朝着正确的方向?
VAR的URL = [];Object.keys(result.items).forEach(函数(项目){
urls.push(https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=+ result.items [项目] .contentDetails
.videoId +&放大器;键= XXX);})async.each(
网址,
请求(URL,功能(呃,响应体){
如果(ERR){
的console.log(ERR);
返回;
}
体= JSON.parse(体);
}),
功能(){
VAR扩展= _.extend(结果,体);
getVideoDetailsCallback(NULL,扩展)
});
看来你调用要求
与回调和所有,而不是只引用它,这意味着你可能需要一个匿名函数调用。
另外,如果你想在年底无论你延伸的阵列,或者,你可以只使用 async.map
代替,并完成类似
VAR的url = Object.keys(result.items).MAP(函数(项目){
回归https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=+ result.items [项目] .contentDetails.videoId +&放大器;键= XXX;
});async.map(网址功能(URL,回调){
请求(URL,功能(呃,响应体){
如果(ERR){
返回回调(ERR);
}
回调(NULL,JSON.parse(体));
});
},功能(呃,扩展){
如果(ERR){
//处理错误
}
//扩展为包含解析JSON数组
getVideoDetailsCallback(NULL,扩展);
});
In my node.js project, I need to make iterative API request so I'm using async.each method.
The console complains that url is not defined and I understand this because I've only declared urls which is an array of url.
I'm just not sure how I can put together request() from request module inside async.each. To satisfy async.each() I've placed urls as the first argument but request() itself requires a query string argument which is url.
I'm also using _.extend from Underscore to merge two responses but I'm not sure where I have it currently is in the right place.
Can someone please point me in the right direction?
var urls = [];
Object.keys(result.items).forEach(function(item) {
urls.push("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=" + result.items[item].contentDetails
.videoId + "&key=xxx");
})
async.each(
urls,
request(url, function(err, response, body) {
if (err) {
console.log(err);
return;
}
body = JSON.parse(body);
}),
function() {
var extended = _.extend(result, body);
getVideoDetailsCallback(null, extended)
});
It seems you're calling request
with callbacks and all, and not just referencing it, which means you probably need an anonymous function call.
Also, if you want an array at the end, or whatever you're extending, you could just use async.map
instead, and do something like
var urls = Object.keys(result.items).map(function(item) {
return "https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=" + result.items[item].contentDetails.videoId + "&key=xxx";
});
async.map(urls, function(url, callback) {
request(url, function(err, response, body) {
if (err) {
return callback(err);
}
callback(null, JSON.parse(body));
});
}, function(err, extended) {
if (err) {
// handle error
}
// extended is an array containing the parsed JSON
getVideoDetailsCallback(null, extended);
});
这篇关于如何在node.js的防爆preSS async.each中使用要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!