Reputation: 19
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;
}
Upvotes: 0
Views: 56
Reputation: 597
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
Upvotes: 0
Reputation: 23798
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 = {};
Upvotes: 2