Return from a simple javascript function not working

0

I have the following code snippet that makes a call to the findCustomers(term, isNumber));

if(isSequenceNumber(term) && term.length >= 6){
    var data = findCustomers(term, true);
    console.log(data);
    response(data);
}else{
    var data = findCustomers(term, false);
    console.log(data);
    response(data);
}

The method that is not returning anything is below:

function findCustomers(term, isNumber){
    $.ajax({
        url: appContext + "/auth/search-customer-by-service",
        type: "GET",
        contentType: "application/json",
        dataType: 'json',
        data: {
            term: term,
            isNumber: isNumber
        },
        success: function(data) {
            if (data.length > 0) {
                // Com resultados de clientes.
                var customerArray = new Array(data.length);
                var i = 0;
                data.forEach(function(entry) {
                    var newObject = {
                        label: entry.fullName + " " + "[" + entry.email + "]",
                        idCustomer: entry.idCustomer,
                        hasAvailableService: entry.hasAvailableService,
                    };
                    customerArray[i] = newObject;
                    i++;
                });
                return customerArray;
            } else {
                // Sem resultados de clientes.
                var notFoundArray = new Array(1);
                var notFoundObject = {
                        label: "Nenhum cliente encontrado",
                        idCustomer: 0,
                        hasAvailableService: "",
                };
                notFoundArray[0] = notFoundObject;
                return notFoundArray;
            }
        }
    });
}

It is worth noting that the request made by AJAX returns concrete data, but the callback of the function is always being undefinied

EDITED

After understanding the nature of the problem based on the answers proposed below, I called the function in which you perform the AJAX request via anonymous function like this:

findCustomers(term, true, function(response){
    console.log(response);
    response(response);
});

But I still have not succeeded.

    
asked by anonymous 29.11.2018 / 17:08

1 answer

1

Do as follows:

if(isSequenceNumber(term) && term.length >= 6){
    var data = findCustomers(term, true, resultado);
}else{
    var data = findCustomers(term, false, resultado);
}

function resultado(data) {
    console.log(data);
    response(data);
}

function findCustomers(term, isNumber, resultado){
    $.ajax({
        url: appContext + "/auth/search-customer-by-service",
        type: "GET",
        contentType: "application/json",
        dataType: 'json',
        data: {
            term: term,
            isNumber: isNumber
        },
        success: function(data) {
            if (data.length > 0) {
                // Com resultados de clientes.
                var customerArray = new Array(data.length);
                var i = 0;
                data.forEach(function(entry) {
                    var newObject = {
                        label: entry.fullName + " " + "[" + entry.email + "]",
                        idCustomer: entry.idCustomer,
                        hasAvailableService: entry.hasAvailableService,
                    };
                    customerArray[i] = newObject;
                    i++;
                });
                resultado(customerArray);
            } else {
                // Sem resultados de clientes.
                var notFoundArray = new Array(1);
                var notFoundObject = {
                        label: "Nenhum cliente encontrado",
                        idCustomer: 0,
                        hasAvailableService: "",
                };
                notFoundArray[0] = notFoundObject;
                resultado(notFoundArray);
            }
        }
    });
}

That is, your result function will actually have what will be done, being a callback only when success of your request $.ajax is called.

PS: Maybe the syntax is not perfect because I wrote right here in the OS, but I believe it will work.

If you were not going to create a new array within success, you could simply change that whole function to% w / o of what it would give it. You can do this routine within your function too, there it is with you.

A good reference: What is callback?

    
29.11.2018 / 20:47