Why function is returning Undefined

Asked

Viewed 564 times

0

I am trying to get a return json of the function and associate it to a variable, because so I take the need to structure the received data with page from within this function and I can work in another function this, but it is not working out the return is 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);
  • See if this answer helps you: http://answall.com/questions/92779/uma-function-n%C3%A3o-recognizes-a-outra/92781#92781

  • I tried here and it didn’t work

2 answers

2

Perhaps because it is an asynchronous request, it tries to put the $Scope variable inside 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;
});
  • tried here man and did not function, the answer on the console is still Undefined

  • you need to make sure that this service ('run/getAllFunctions') is returning something from the server. You can see this in Chrome’s Developer Tools or Mozilla’s Firebug, or by testing it with some browser plugin, such as Chrome’s Postman. In Chrome Developer tools you can see in the Network tab.

  • it is returning, already tested, it returns a json, but I needed to take the logic from within consume service to treat in another function

2


lucianohdr has already sung the ball. This request you are trying to make is asynchronous. You will need to use a callback method to treat.

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);
    });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.