简体   繁体   English

NodeJS HTTP请求未按顺序执行

[英]NodeJS HTTP Requests not executing in order

First post but thanks everyone for all the info! 第一篇文章,但感谢所有人的所有信息!

On to the issue. 关于这个问题。 I have some code in which I am trying to iterate over a JSON file and execute an HTTP Get Request on each object in the array. 我有一些代码,我试图迭代JSON文件并对数组中的每个对象执行HTTP Get Request。 The issue seems to arise in that when I am executing the http get request that it does not do so in order nor does it complete. 问题似乎出现在我执行http get请求时,它不是按顺序执行,也不是完成。 It hangs up after about 6-9 calls against my API. 它在我的API大约6-9次调用后挂起。

Sample JSON: 示例JSON:

    [
  {
    "Name": "ActClgStpt",
    "Address": 326,
    "Slot": 1
  },
  {
    "Name": "ActHtgStpt",
    "Address": 324,
    "Slot": 1
  },
  {
    "Name": "AdvanceCool",
    "Address": 21,
    "Slot": 1
  }
]

Iterating over the JSON: 迭代JSON:

    sedona.jsonInputAddress('Unit1GWRenton', logMe);

function logMe() {
    for(var i in config)
    {

        var name = config[i].Name;
        var address = config[i].Address;
        var slot = config[i].Slot;
        console.log(name + " " + address + " " + slot);
        sedona.collectValues("192.168.101.14", 2001, config[i].Name, config[i].Address, config[i].Slot,function(){console.log("Done")})
    }


}

Copy of the function I am executing on each loop for the API call. 我在API调用的每个循环上执行的函数的副本。 I have a callback set but I don't think I may have set this up properly: 我有一个回调设置,但我认为我可能没有正确设置它:

collectValues:function(site,port,name,address,slot,callback){

    /* Build Scrape Constructor */
    var url = 'http://' + (site) + ':' + (port) + '/twave/app/' + (address);

    /* Slice out Unit # */
    unitNumber = port.toString().slice(2, 4);

    /* Create slotid */
    var slotmaker = "slot" + (slot);

    /* Get ISO Timestamp */
    var dt = new Date();
    var isoDate = dt.toISOString();
    isoTime = isoDate.slice(0,19).replace('T', ' ').concat('.000000+00:00');

    /* Make API Call */
    request.get({
        agent: false,
        url: url,
        json: true
    }, function response (error, response, body) {
        if (!error && response.statusCode === 200) {

            // Grab Point Name
            pointname = name;

            // Grab Point Value
            var value = body.slots;
            var slot = value[slotmaker];
            slotvalue = slot.value;

            // Testing Logs
            console.log(isoTime + " " +pointname + " " + slotvalue);

            callback()



        }
    });



}

Sample of my console log where it hangs up: 挂起的控制台日志示例:

ActClgStpt 326 1
ActHtgStpt 324 1
AdvanceCool 21 1
AdvanceDewEnable 462 1
CO2Sensor 455 1
CO2Stpt 257 1
CTRange 14 6
ComfortStatus 328 1
CompAllow 167 1
Cool1Spd 83 1
Cool2Spd 84 1
CoolCall1 314 2
CoolCall2 315 2
CoolCmd1 109 1
CoolCmd2 110 1
DCVMaxVolume 260 2
DCVResponse 502 2
SaTemp 423 1
DaTempLimit 193 2
Damper 387 1
DriveFaultCode 123 4
ESMEconMin 175 1
ESMMode 8 1
EconDewEnable 464 1
EconMode 96 1
EconTest 496 1
FanCall 78 1
FanPower 491 1
FanSpeed 492 1
FanStatus 135 1
FullSpd 38 1
Heat1Spd 31 1
Heat2Spd 32 1
HeatCall1 316 2
HeatCall2 317 2
HeatCmd1 69 1
HeatCmd2 70 1
HighAlarmStpt 62 1
HighAlertStpt 61 1
LowAlarmStpt 59 1
LowAlertStpt 58 1
OSAVolume 493 1
OaTemp 457 1
OccClgStpt 247 1
OccHtgStpt 246 1
Occupied 313 1
OptimumStartCommand 233 1
OverrideTime 348 1
PBStatus 221 1
PowerExCmd 107 1
PowerExStpt 188 1
RaTemp 456 1
ResetDrive 212 1
ServiceSwitch 361 5
SoftSwitch 310 4
SpaceTemp 490 1
StdEconMin 176 1
StdEconStpt 307 1
StptAdj 291 1
StptAdjRange 269 1
UnitAmps 454 1
UnitHealth 276 2
UnoccClgStpt 268 1
UnoccHtgStpt 258 1
VentMode 400 2
VentSpd 30 1
2016-01-04 16:40:15.000000+00:00 ActClgStpt 73.000000
Done
2016-01-04 16:40:15.000000+00:00 UnitAmps 5.406000
Done
2016-01-04 16:40:15.000000+00:00 CoolCmd2 false
Done
2016-01-04 16:40:15.000000+00:00 ActHtgStpt 68.000000
Done

Anything you think I can improve on in the code that would be great.. Still learning everyday on Node! 你认为我可以在代码中改进的任何东西都会很棒..还是每天都在Node上学习!

Per Anand S, it looks like non-200's will hang you. Per Anand S,看起来非200会让你感到困惑。 Change this code: 更改此代码:

        console.log(isoTime + " " +pointname + " " + slotvalue);
        callback()
    }

into this: 进入这个:

        console.log(isoTime + " " +pointname + " " + slotvalue);
    }
    callback()

and you should stop hanging. 你应该停止。

As for out-of-order, the request.get() call only queues up a request, it doesn't actually make the request. 至于乱序, request.get()调用只排队请求,它实际上不会发出请求。 That has to wait for the event loop to fire up again, which won't occur until the calling function returns. 这必须等待事件循环再次启动,这将在调用函数返回之前发生。 By that time, there could be another request.get() also queued that might sneak ahead of it (or, more precisely, may call its callback before the former one). 到那个时候,可能还有另一个request.get()也排队等待它可能潜行(或者更准确地说,可以在前一个之前调用它的回调)。

I usually handle order issues by having an array that each callback will write into (with a unique index). 我通常通过使用每个回调将写入的数组(使用唯一索引)来处理订单问题。 While less efficient, you can also use async series-type functions to order requests. 虽然效率较低,但您也可以使用async系列类型函数来订购请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM