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.