Why function is returning undefined [closed]

0

I'm trying to get a json to return the function and associate it with a variable, so I take the need to structure the data received with the page from within that function and I can work on another function, but it's not working. being undefined , see the code

var token = getCookie("token");
var json = {};
$scope.query = consumeService (token, JSON.stringify(json), "funcionario/getAllFuncionarios", "POST", "alerta", function(result){
    var r = result;
    return r;
});

console.log("Query :" +$scope.query);
    
asked by anonymous 27.10.2015 / 18:07

2 answers

2

The lucianohdr already sang the ball. This request you are trying to make is asynchronous. You'll need to use a callback method to handle.

function chamar(){
    consultar(function(result){
        console.log(result);
    });
}

function consultar(callback) {
    var token = getCookie("token");
    var json = {};
    consumeService (token, JSON.stringify(json), "funcionario/getAllFuncionarios", "POST", "alerta", function(result){
        callback(result);
    });
}
    
27.10.2015 / 18:21
2

Perhaps because it is an asynchronous request, it tries to put the $ scope variable inside the function:

var token = getCookie("token");
var json = {};
consumeService (token, JSON.stringify(json), "funcionario/getAllFuncionarios", "POST", "alerta", function(result){
    var r = result;
    console.log("Result :" + r);
    $scope.query = r;
});
    
27.10.2015 / 18:14