我是 nodeJs 的新手。在 for 循环中,从某个 url 获取数据后,我得到了未定义的 array[i] 值。我不知道如何解决这个问题。请帮我。
这是我的代码
urls =['url1','url2','url3','url4']
for (var i = 0; i < urls.length; i++) {
console.log(urls[i]) // getting value
var opt = {
url :urls[i],
headers: {
'Authorization': 'Bearer '+reply
}
}
request.get(opt,function(error,response,body) {
console.log(urls[i]) // getting undefined
if(!error) {
enrichment_data.push({type:body});
} else {
enrichment_data.push({type:''})
}
})
}
Run Code Online (Sandbox Code Playgroud)