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

Massimo ScolaSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PANkaj KumarSoftware DeveloperCommented:
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 EngineerAuthor Commented:
JSON DATA:

MEGAMAX.png
PANkaj KumarSoftware DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Massimo ScolaSoftware EngineerAuthor 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 DeveloperCommented:
We need to see the allClients array structure.
PANkaj KumarSoftware DeveloperCommented:
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 EngineerAuthor 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 EngineerAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.