0

I am calling the function Allow() in html code, if returns true, will allow the user to Edit the data.

The functions $scope.GetCurrentStatus(), $scope.GetCurrentStatus() both are synchronous.

But it returns the following error in Allow() method.

Type Error: Cannot read property 'UserType' of undefined

Help me to handle the situation.

//LoadStatus
$scope.GetCurrentStatus = function () {
    var defer = $q.defer();
    $http({
        method: "POST",
        url: "../Documentation/Documentation.aspx/GetStatus",
        data: {}
    })
    .then(function (response) {
       defer.resolve(response.data.d);
        $scope.Status = response.data.d;
    }),
    function (error) { data, status };
}

$scope.GetCurrentStatus();

//Load AccessRole 
$scope.GetAccessRole = function () {
    var defer = $q.defer();
    $http({
        method: "POST",
        url: "../Documentation/Documentation.aspx/GetAccessRole",
        data: {}
    })
   .then(function (response) {
       defer.resolve(response.data.d);
       $scope.Access = response.data.d;
   }),
   function (error) { data, status };
};

$scope.GetAccessRole();

$scope.Allow = function () {
    if ($scope.Access.UserType == 'Admin' || !($scope.Status.Status == 'CC' || $scope.Status.Status == 'CD'))
        return true;
    return false;
}
3
  • Is your function GetAccessRole() returning the data you want? Keep console.log($scope.Access) there. Commented Dec 21, 2017 at 7:20
  • How can GetCurrentStatus and GetAccessRole are synchronous. You are using promises which made them async. By the time you are calling Allow(), you havent got your response. Commented Dec 21, 2017 at 7:23
  • Yes GetAccessRole() returns data. But $scope.Allow() executed before GetAccessRole() returns data. Commented Dec 21, 2017 at 7:23

2 Answers 2

2

Although the functions $scope.GetCurrentStatus() and $scope.GetCurrentStatus() are called in a synchronous fasion, they have some asynchronous code in them.

Unfortunately your variable $scope.Access and $scope.Status are created only inside these asynchronous methods. So, it takes some waiting for a network response until the time comes to set your scope variables.

One workaround is to declare $scope.Access and $scope.Status somewhere upper in the controller.

$scope.Access = {};
$scope.Status = {};
Sign up to request clarification or add additional context in comments.

Comments

0

This is happening because you are using $scope.Allow before setting actual $scope.Access variable set by any value due to async call. So you can call $scope.Allow method inside defer success or set $scope.access and $scope.status before calling $scope.Allow method

   $scope.Access = {};
   $scope.Status = {};
   $scope.GetCurrentStatus() = function(){
       return $http.get('http://UR_URL');
   });
   $scope.GetAccessRole = function(){
       return $http.get('http://UR_URL');
   });

And use as

 $scope.GetCurrentStatus().then(function(response){
   $scope.Status = response.data.d;
 });
 $scope.GetAccessRole().then(function(response){
   $scope.Access = response.data.d;
 });

and then call $scope.Allow method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.