0

I want to use foreach on data that is returned from my another function: I am using below:

function getAllCompetitors() {
    console.log('In getAllCompetitors function js file');
    return $.ajax({
        url: '/salescrm/getAllCompetitors',
        type: 'POST',
        success: function(data) {
            console.log('getAllCompetitors data: ',data);
            //response(data);
            return data;
        },
        error: function(error) {
            console.log('data error: ',error);
        }
    });
}

Below is my foreach:

getAllCompetitors.foreach(function(item){

})

Its showing error.

I want, to use foreach on data that is returned from function. Can anybody assist me to solve this.

Thanks in advance.

Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
rohit13807
  • 605
  • 8
  • 19
  • 2
    `getAllCompetitors()` returns the ajax call, *not* the data from the success handler. If you want to parse the data then you do it either inside the success handler, or you call another function from there and pass the data as a parameter. – Reinstate Monica Cellio Aug 08 '18 at 12:28
  • 1
    ^^ ...or use the fact that the `jqXHR` object is Promise-like (and Promise-compatible in modern versions of jQuery) and use `then`/`catch` on it (or even `async`/`await` in cutting-edge environments or via transpilation). Also note that JavaScript is case-sensitive, `foreach` != `forEach`. – T.J. Crowder Aug 08 '18 at 12:30

0 Answers0