Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于,使用数组和do Request()Node.js_Javascript_Arrays_Node.js - Fatal编程技术网

Javascript 对于,使用数组和do Request()Node.js

Javascript 对于,使用数组和do Request()Node.js,javascript,arrays,node.js,Javascript,Arrays,Node.js,我有这段代码,我想对数组中的每个对象执行request()。我是这样想的。但它不起作用 var arrayLength = Temporal.length; //Temporal it's the name of the array for (var i = 0; i < arrayLength; i++) { var id = Temporal[i].market_hash_name; request('http://steamcommunity.com/mark

我有这段代码,我想对数组中的每个对象执行request()。我是这样想的。但它不起作用

  var arrayLength = Temporal.length; //Temporal it's the name of the array
  for (var i = 0; i < arrayLength; i++) {
    var id = Temporal[i].market_hash_name;
    request('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='+id,
      function (e, r, body){
        var req_data = JSON.parse(body);
        var lowPrice = req_data['lowest_price'];
        Temporal[i].price = lowPrice;
      }
    );
  }

不清楚您观察到了什么问题,但我已经看到:

Temporal[i].price = lowPrice;
将不起作用,因为
request()
回调将在将来的某个时间调用,这是在
for
循环完成且
i
位于数组末尾的值之后很久。最简单的更改是使用
.forEach()
迭代数组,而不是使用
for
循环:

  Temporal.forEach(function(item, i) {
    var id = item.market_hash_name;
    request('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='+id, function (e, r, body) {
        var req_data = JSON.parse(body);
        var lowPrice = req_data['lowest_price'];
        item.price = lowPrice;
      }
    );
  });
在node.js的当前版本中,您还可以将
for
循环与
let i
一起使用,而不是
var i
,或者可以使用
for/of
循环

有关此一般问题和可用选项的更多信息,请参阅:


如果您还想知道所有的
request()
异步操作何时完成,那么有很多方法可以做到这一点。无需进一步重写代码,只需添加一个计数即可

  let requestsDone = 0;
  Temporal.forEach(function(item, i) {
    var id = item.market_hash_name;
    request('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='+id, function (e, r, body) {
        var req_data = JSON.parse(body);
        var lowPrice = req_data['lowest_price'];
        item.price = lowPrice;
        ++requestsDone;
        // check to see if all requests are done
        if (requestsDone === Temporal.length) {
            // here, you know that all requests are done and you can
            // use that result in here
        }
    );
  });
仅供参考,您还需要对
request()
操作进行错误处理

当您的所有请求都完成时,一种更复杂的跟踪方法是使用
request promise
模块,而不是
request
,然后使用
promise.all()
。你可以在这里看到它是如何工作的:


我建议您使用名为axios的npm模块,这样做很简单
首先通过键入下载模块:

npm install axios --save
然后,要请求每个对象数组,请使用以下命令:

var axios=require('axios')
var array=Temporal
array.forEach(function(data){
 axios.get('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='+data.market_hash_name)
.then(function(body){
 data.price=body.data.lowest_price
 })
})
  setTimeout(function(){
console.log(array)      
 },1000)

您正在for循环的每个迭代中执行异步操作,当执行每个回调操作(您传递给
请求(url,callback)
)时,循环已经结束

使用
forEach()
至少会更新数组,但在更新之前,执行将继续(异步操作仍在运行)。如果要在循环后使用结果,这是不可取的行为

我建议您使用Promises或一些库来实现异步控制流(如)

示例使用


如果您想使用,学习曲线比使用回调更高,但是学习如何使用它们是值得的。如果你需要一个承诺的例子,请告诉我。

什么是“它不起作用”呢?你期望的结果是什么?你观察到的结果是什么?你到底需要帮助解决什么问题?因为看起来你可能是新来的,将来,“它不起作用”不是stackoverflow上一个好问题的标志。你应该描述你期望它如何工作,你应该描述你所观察到的不是那样的事情,您应该描述您为了解正在发生的情况而采取的调试步骤,然后非常具体地说明您需要帮助的内容。经过编辑的代码应该可以工作,但您的
价格
键将异步填充。您应该等待所有响应才能完全填满数组。添加了有关知道何时完成所有请求的信息。@ElvisRamirez-这是否回答了您的问题?如果是这样,您可以通过单击答案左侧的绿色复选标记向社区表明这一点,这也将为您赢得一些声誉点数。
var axios=require('axios')
var array=Temporal
array.forEach(function(data){
 axios.get('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='+data.market_hash_name)
.then(function(body){
 data.price=body.data.lowest_price
 })
})
  setTimeout(function(){
console.log(array)      
 },1000)
var each = require('async/each');

async.each(Temporal, function(element, done) {
  var id = element.market_hash_name;
  request('http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='+id,
    function (e, r, body) {
      var req_data = JSON.parse(body);
      var lowPrice = req_data['lowest_price'];
      Temporal[i].price = lowPrice;
      done();
    }
  );
}, function (err) {
  // continue execution here, at this point all the operations are done
});