CodeCanyon CodeCanyon - 5 months ago 11
AngularJS Question

Angularjs TypeError: UserService.getCurrentUser.then is not a function at m.$scope.getCurrentUserId

I have an Angular conreoller and an Angular service.From my controller, i'm calling to the service.There are two functions in my Service.First function works fine.But this problem occurs with the second one.Following is the code for the relevant function.

function getCurrentUser() {
return $resource(_URLS.BASE_API + 'get_current_user' + _URLS.TOKEN_API + $localStorage.token).get().$promise;
}


Following the code for calling that service's function from the controller,

//Get current user id
$scope.getCurrentUserId = function() {
ParentService.getCurrentUser.then(function(response) {
if (response.query_status == "success") {
$scope.userid = response.data.id;
console.log('Value is: '+ $scope.userid);
}
else {
$scope.userid = null;
}
}, function(error) {
$scope.userid = null;
console.log("Error : Failed to load user data...");
console.log(error);
});
}


I can't figure out what's the issue is since my first function works fine.So hope someone will help me to solve this issue.

Answer

Your controller code should be as following. Notice the () in ParentService.getCurrentUser()

$scope.getCurrentUserId = function() {
    ParentService.getCurrentUser() // notice the "()" here
    .then(function(response) {
        ...
    }, function(error) {
        ...
    });
};
Comments