We help IT Professionals succeed at work.

JavaScript issue with concurrency

I am developing an app using Apache Cordova and I have the following code which fails to execute. The idea of the code is:

1) loop through the array with all orders (JSON)
2) retrieve the customer's name
3) generate a table or <tr>

The problem is point 2 - the code just stops and I have to restart the server.
The array with orders is not that big - around 150 JSON objects which I retrieve when the application loads;
the array with customers is even smaller: just two clients are in the array.

Is this an issue with JavaScript concurrency? I would appreciate it if someone can give me an idea of how to tackle this, as I have spent hours trying to figure out how to make this work.

Surely, it must be possible to retrieve the orders - then lookup the customer's name - and then put the data into another array.

Thanks

Massimo

      /**
       * @description retrieves all orders for megamax and genereates table rows for the HTML table
       */
      this.getAllMegamaxOrders = function () {
        "use strict";
        var mm_orderID = 0;
        var mm_customer_id = "0";
        var mm_orderDate = "";
        var mm_customerName = "";
        var mm_lat = "";
        var mm_lng = "";
        var mm_price = "";
      
        
        //loop through all orders from array allOrders
        for (i = 0; i < app.allOrders.length; i++) {

          //populate local variables
          mm_orderID = app.allOrders[i].id;
          mm_customer_id = app.allOrders[i].client_id;
          
          //get the customer's name -----> FAILS
          for (i = 0; i < app.allClients.length; i++) {
            if (app.allClients[i].id == mm_customer_id){
              console.log("FOUND");
            }
          }
          mm_orderDate = app.allOrders[i].date;
          mm_lat = app.allOrders[i].latitude;
          mm_lng = app.allOrders[i].longitude;

          //mm_price = app.megamax.calculateOrderTotal(mm_orderID);
        

          //create markup for table
          var markup = '<tr><td>' + mm_orderID + '</td><td>' + mm_orderDate + '</td><td>' + mm_customerName + '</td><td><button type="button" class="btn btn-primary btn-sm" value= ' + mm_lat + ',' + mm_lng + '>Location</button></td></tr>';
          app.allMegamaxOrders_HTML.push(markup);
        }
      };

Open in new window

Comment
Watch Question

PANkaj KumarSoftware Developer

Commented:
Can you share JSON data.

Your code seems to correct,

Update inner for loop

//get the customer's name -----> FAILS
        for (var j = 0; j < app.allClients.length; j++) {  // updated
            if (app.allClients[j].id == mm_customer_id) {
                console.log("FOUND");
            }
        }

Open in new window

Massimo ScolaSoftware Engineer

Author

Commented:
JSON DATA:

MEGAMAX.png
Software Developer
Commented:
Massimo, use following code:

       /**
       * @description retrieves all orders for megamax and genereates table rows for the HTML table
       */
      this.getAllMegamaxOrders = function () {
        "use strict";
        var mm_orderID = 0;
        var mm_customer_id = "0";
        var mm_orderDate = "";
        var mm_customerName = "";
        var mm_lat = "";
        var mm_lng = "";
        var mm_price = "";
      
        
        //loop through all orders from array allOrders
        for (i = 0; i < app.allOrders.length; i++) {

          //populate local variables
          mm_orderID = app.allOrders[i].id;
          mm_customer_id = app.allOrders[i].client_id;
          
          //get the customer's name -----> UPDATED
          for (var j = 0; j < app.allClients.length; j++) {
            if (app.allClients[j].id == mm_customer_id){
              console.log(app.allClients[j].Name);    // update field name
            }
          }
          mm_orderDate = app.allOrders[i].date;
          mm_lat = app.allOrders[i].latitude;
          mm_lng = app.allOrders[i].longitude;

          //mm_price = app.megamax.calculateOrderTotal(mm_orderID);
        

          //create markup for table
          var markup = '<tr><td>' + mm_orderID + '</td><td>' + mm_orderDate + '</td><td>' + mm_customerName + '</td><td><button type="button" class="btn btn-primary btn-sm" value= ' + mm_lat + ',' + mm_lng + '>Location</button></td></tr>';
          app.allMegamaxOrders_HTML.push(markup);
        }
      };

Open in new window

Massimo ScolaSoftware Engineer

Author

Commented:
At least, I am not getting an error message but I fail to get the console output "FOUND".

megamax2.png
this.getAllMegamaxOrders = function () {
        "use strict";
        var mm_orderID = 0;
        var mm_customer_id = "0";
        var mm_orderDate = "";
        var mm_customerName = "";
        var mm_lat = "";
        var mm_lng = "";
        var mm_price = "";
      
        
        //loop through all orders from array allOrders
        for (i = 0; i < app.allOrders.length; i++) {

          //populate local variables
          mm_orderID = app.allOrders[i].id;
          mm_customer_id = app.allOrders[i].client_id;
          
          // //get the customer's name -----> FAILS
         for (var j = 0; j < app.allClients.length; j++) {  // updated
              if (app.allClients[j].id == String(mm_customer_id)) {
                  console.log("FOUND");
              }
          }
          mm_orderDate = app.allOrders[i].date;
          mm_lat = app.allOrders[i].latitude;
          mm_lng = app.allOrders[i].longitude;

          //mm_price = app.megamax.calculateOrderTotal(mm_orderID);
        

          //create markup for table
          var markup = '<tr><td>' + mm_orderID + '</td><td>' + mm_orderDate + '</td><td>' + mm_customerName + '</td><td><button type="button" class="btn btn-primary btn-sm" value= ' + mm_lat + ',' + mm_lng + '>Location</button></td></tr>';
          app.allMegamaxOrders_HTML.push(markup);
        }
      };

Open in new window

Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
We need to see the allClients array structure.
PANkaj KumarSoftware Developer
Commented:
Massimo,

It may be related to datatype.


console.log(app.allClients[j].id);
console.log(mm_customer_id); 
If(app.allClients[j].id == String(mm_customer_id)) {
                  console.log("FOUND");
              }
          }

Open in new window


Share output of above code
Massimo ScolaSoftware Engineer

Author

Commented:
Oh my god, you guys are geniouses!

I found the problem - actually two of them!

a) what helped most was for (var j = 0; j < app.allClients.length; j++)
b) the array with clients was empty - I had commented it out for some reason last night
c) the data type was also an issue

now it works like a charm!
Massimo ScolaSoftware Engineer

Author

Commented:
My problem was that I did not iterate through the array correctly.

 for (var j = 0; j < app.allClients.length; j++)

is the right way to iterate through an array. I didn't have the var j in it.