我希望将一个变量绑定到我的请求对象,所以当回调发生时,我可以访问这个变量.
这是图书馆:https: //github.com/request/request
这是我的代码.
var request = require('request');
for (i = 0; i < cars.length; i++) {
request({
headers: { 'Content-Type': 'application/json'},
uri: 'https://example.com',
method: 'POST',
body: '{"clientId": "x", "clientSecret": "y"}'
},
function(err, res, body){
// I want to put the correct i here.
// This outputs cars.length almost everytime.
console.log(i);
});
}
Run Code Online (Sandbox Code Playgroud)