小编Jim*_*ray的帖子

节点js将变量传递给请求回调函数

我希望将一个变量绑定到我的请求对象,所以当回调发生时,我可以访问这个变量.

这是图书馆: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)

javascript asynchronous node.js

3
推荐指数
1
解决办法
3770
查看次数

标签 统计

asynchronous ×1

javascript ×1

node.js ×1