1

I've got a JavaScript function that loops over a set of devices, makes an API call to that device and returns results in JSON format. However, the results that are returned and printed to the screen are the same for all devices (even though they aren't supposed to be).

I've verified that the API call is hitting each different device on the server-side, yet still, the response shows like it's all from the same device.

From everything I've read this has to do with scopes but I cannot for the life of me get this to work any ideas?

JavaScript AJAX function: (UPDATED)

function getEthernetSwitchingTable(devices, macAddr) {
    for (i=0;i<devices.length;i++) {
        (function(idx){

            var hostname = devices[idx]['hostname']
            var deviceId = devices[idx]['id']

            var URL = '/api/v1/device/'+hostname+'/ethernet-switching-table.json?macAddr='+macAddr

            var ajax = $.get(URL, function(res){
                console.log(hostname)
                console.log(res)
            }, "json")

        })(i);
    }
}

Server-side logs:

"GET /api/v1/device/cr01.corp.ldn01/ethernet-switching-table.json?macAddr=00:07:4d:55:d5:da HTTP/1.1" 200 317
"GET /api/v1/device/cr02.corp.ldn01/ethernet-switching-table.json?macAddr=00:07:4d:55:d5:da HTTP/1.1" 200 317
"GET /api/v1/device/cr02.plant.ldn01/ethernet-switching-table.json?macAddr=00:07:4d:55:d5:da HTTP/1.1" 200 317
"GET /api/v1/device/cr01.plant.ldn01/ethernet-switching-table.json?macAddr=00:07:4d:55:d5:da HTTP/1.1" 200 317

Console response:

cr01.corp.ldn01
{DeviceHostname: "cr02.plant.ldn01", DeviceId: 104, EthernetSwitchingTable: {…}, Status: 200}
cr02.corp.ldn01
{DeviceHostname: "cr02.plant.ldn01", DeviceId: 104, EthernetSwitchingTable: {…}, Status: 200}
cr02.plant.ldn01
{DeviceHostname: "cr02.plant.ldn01", DeviceId: 104, EthernetSwitchingTable: {…}, Status: 200}
cr01.plant.ldn01
{DeviceHostname: "cr02.plant.ldn01", DeviceId: 104, EthernetSwitchingTable: {…}, Status: 200}

Expected response:

cr01.corp.ldn01
{DeviceHostname: "cr01.corp.ldn01", DeviceId: 57, EthernetSwitchingTable: null, Status: 200}
cr02.corp.ldn01
{DeviceHostname: "cr02.corp.ldn01", DeviceId: 100, EthernetSwitchingTable: null, Status: 200}
cr02.plant.ldn01
{DeviceHostname:"cr02.plant.ldn01", DeviceId:104, EthernetSwitchingTable:{"l2ng-l2-mac-vlan-name":[{"data":"pack11-15-304"}]}, Status:200}
cr01.plant.ldn01
{DeviceHostname: "cr01.plant.ldn01", DeviceId: 59, EthernetSwitchingTable: null, Status: 200}
Devin
  • 1,011
  • 2
  • 14
  • 30
  • 2
    Your requests are asynchronous, assuming `$.get` refers [to jQuery's implementation](https://api.jquery.com/jquery.get/). – Heretic Monkey Dec 30 '19 at 21:38
  • can you give us your device array of objects... – EugenSunic Dec 30 '19 at 21:43
  • unclear how that would produce the same result coming back. Is that `$.get` jQuery or some custom method? you really should declare `i` so it is not global – epascarello Dec 30 '19 at 21:46
  • `[{id: 57, hostname: "cr01.corp.ldn01"},{id: 100, hostname: "cr02.corp.ldn01"},{id: 59, hostname: "cr01.plant.ldn01"},{id: 104, hostname: "cr02.plant.ldn01"}]` – Devin Dec 30 '19 at 21:49
  • `$.get` is jQuery – Devin Dec 30 '19 at 21:49
  • 1
    I would look at the console and look at the requests in the network tab and see what is going on. My guess is the server is returning the same data. Clear your cache and make sure it is not some old data from a bad request you made in the past with the server. – epascarello Dec 30 '19 at 21:54
  • 1
    You should be able to see the Ajax responses in dev tools network tab, are they all identical there? Also I noticed you wrapped a couple of things in (IMO unnecessary) IIFEs, perhaps get rid of those. – James Dec 30 '19 at 21:59
  • the responses in the Dev Tools network tab do reflect being all the same, which is strange because when I make each individual request separately, the response from the server is accurate so it's not the server-side code. – Devin Dec 30 '19 at 22:05
  • @Gerard it is generated by the script. and matches the Network response in Dev tools. However, if I run each API request one-by-one, the responses are correct. In other words, they are not the same. It seems like the response is getting overwritten on the client-side somehow. – Devin Dec 30 '19 at 22:19
  • @Devin you are making asynchronous requests in synchronour way. You have two options. (1) use promises or (2) please refer the link https://stackoverflow.com/questions/22621689/javascript-ajax-request-inside-loops/22621845 – sam Dec 30 '19 at 22:23
  • @sam I did give something like this a try but will try again based on the post you shared and update – Devin Dec 30 '19 at 22:52
  • it looks like a cache problem, the code on the server should put an immediate expiration date – Mister Jojo Dec 30 '19 at 23:05

0 Answers0