NodeJS HTTP requests do not execute in order

First post, but thanks to everyone for all the information!

About the problem. I have code in which I try to iterate over a JSON file and execute an HTTP Get Request for each object in an array. The problem seems to be that when I execute the http get request, it does not do this in order and does not complete. It freezes after 6-9 calls against my API.

JSON example:

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

JSON iteration:

  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")}) } } 

A copy of the function that I execute in each loop to call the API. I have a callback set up, but I don’t think I might have configured it correctly:

 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() } }); } 

An example of my console log where it hangs:

 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 

Everything that you think I can improve in code that would be great. Still learning everyday at Node!

+5
source share
1 answer

Per Anand S, the non-200 seems to hang you. Change this code:

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

in it:

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

and you must stop hanging.

As for the non-standard order, calling request.get() only queues the request; it does not actually make the request. This should wait until the event loop starts again, which will not happen until the call function returns. By that time, there may be another request.get() also queued, which can sneak in front of it (or, more precisely, can call its callback to the first).

I usually handle order issues, having an array into which each callback will be written (with a unique index). Although they are less efficient, you can also use functions like async to organize your queries.

+1
source

Source: https://habr.com/ru/post/1239814/


All Articles