-1

There is this small function that should logically returns always, but I am getting undefined :-

    function hasAccess() {
            var dataObj = {
                    "id" : $scope.groupId
                };
            $http.post('http://vlinux:9099/GetItems', dataObj).then(
                    function(response) {
                        var result = response.data.result;
                            if(result.includes($scope.screenId)) {
                                return "ok";
                            } else {
                                return "nok";
                            }
                    });
        }

I started getting downvotes, so wuickly adding, I debugged it and saw http call is bringing expected response and flow is jumping to the right if/else block. Problem is when I am calling this function in a variable its storing undefined.

The call is simple too :-

    var allow = hasAccess();
Soham Banerjee
  • 117
  • 1
  • 13

2 Answers2

0

$http.post is not synchronous, but asynchronous. Thus, all that you have after $http.post is a promise, not the boolean you are expecting.

The documentation show wells how to provide a "callback" to your function, in case of success as well as failure :

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    // for example : manageWHenOk
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
   // for example : manageWHenKO
  });
Marvin
  • 1,650
  • 4
  • 19
  • 41
  • But while debugging I can see the response contains data, moreover it is jumping to right return blocks @Marvin – Soham Banerjee Jun 08 '18 at 15:28
  • the response, contains data, but at the time you get the data, the function is already exited. The return you provide takes place when the response occurs, but it is a callbac. THus, it will occur only once the response has been received. And if you have network issues, this could occur far later! – Marvin Jun 08 '18 at 16:03
0

Your function return a result only if the request is fine. If there is an error your function do nothing! I suggest you to add a catch function.

function hasAccess() {
        var dataObj = {
                "id" : $scope.groupId
            };
        $http.post('http://vlinux:9099/GetItems', dataObj).then(
                function(response) {
                    var result = response.data.result;
                        if(result.includes($scope.screenId)) {
                            return "ok";
                        } else {
                            return "nok";
                        }
                }).catch(function (error) {
                            // do something
                });
    }

If you still have undefined it means that your template is loaded before to obtain the response. In order to resolve that you can do the request in the resolve method of the $stateProvider.

Giuliano
  • 26
  • 1
  • 4