Recursive function for parse array of objects in NodeJS

i have a NodeJS controller like this:

     var importOrders = function (req, res, next) {

    var orders = req.body;

    var processOrder = function (order) {
        if(i<orders.length-1){
            try {
                if (orders[i].Order.Company === "Mondial") {
                    parseMondial(db, orders[i].Order, processOrder(orders[i++]), log);
                }
            } catch (error) {
                next(error);
                log.error('orders', 'error Importing', error, order);
            }
        }
    };

    var i = 0;
    processOrder(orders[i]);
    res.send(orders.length);

};

Inside parseMondial i do lot of queries in mongoDB with promises but this queries its relationship between them.

This doesnt work because i need to finish process inside parseMondial for do the next object parse, but doesnt wait. I dont have idea how can i do it this..Any help its wellcome.

I threw some javascript into a plunkr to emulate what it would be like. Just open up the console and run the code. You should see it going through and returning the data of Mondial Orders.

http://plnkr.co/edit/22qyZox6WJLsv5qS2n11?p=preview

var req = {};
//Emulate orders in req
req.body = [
    {Order: {Type: 'Mondo Cool', Company: 'Mondial'}},
    {Order: {Type: 'Moo', Company: 'Cow Guys'}},
    {Order: {Type: 'Woof', Company: 'Dog Guys'}},
    {Order: {Type: 'Mondo Bacon', Company: 'Mondial'}},
    {Order: {Type: 'Bawk Bawk', Company: 'Cat Guys'}},
    {Order: {Type: 'Mondo Cheese', Company: 'Mondial'}},
];

//Fill in a mock db
var db = 'mockDb';

//Import Orders
var importOrders = function(req, res, next){
    var orders = req.body;
    var processOrder = function () {
      if(i < orders.length){
          if(orders[i].Order.Company === "Mondial"){
            parseMondial(db, orders[i].Order, processOrder);
            i++;
          } else{
            i++;
            processOrder();
          }
      } else {
        //Mocks ending the loop and perform final res.send()
         console.log('res.send');
      }
    };
    var i = 0;
    processOrder();
};

//Emulates parseMondial and async request/queries
function parseMondial(db,order,callback,log){
  //Do something with order asyncrounously
  setTimeout(function(){
    console.log(order);
    console.log('Did a query and waited for the response ' + order.Company);
    callback();
  }, 1000);
}

//Mock making a req to importOrders
importOrders(req,null,null);  

Once the code in parseMondial completes, you can call the callback and if you need to you can pass data. However, it may not have reference to i or orders within parseMondial, so to be safe, I handled passing the orders and iterating the index outside of parseMondial.

Passing in processOrders() isn't a callback. Passing processOrders provides it as a function that can be called at the end of parseMondial.

I took out some of the try/catch stuff just to make the code simpler to read and use to your purposes.

Finally i did this:

var importOrders = function (req, res, next) {

    var orders = req.body;
    var i = 0;

    var doNext = function () {
        i++;
        processOrder();
    };

    var processOrder = function () {
        if (i < orders.length) {
            try {
                if (orders[i].Order.Company === "Mondial") {
                    parseMondial(db, orders[i], doNext, log);
                }
            } catch (error) {
                next(error);
                log.error('orders', 'error Importing', error, orders[i].Order);
            }
        }else{
            res.send({total:i});
        }
    };
    processOrder();
};

And inside parseMondial, when i finish all the tasks i do calback();