mar14
mar14

Reputation: 85

AngularJS: TypeError: Cannot read property 'then' of undefined

First, I'm new in Angular. I prepare some Web Api and I want get some data from them. my service function get date (works fine):

var _getRole = function () {
    $http.get(serviceBase + 'api/User/CurrentUserRoles').then(function (results) {
        return results;
    });
};

and controller:

var role = [];
authService.getRole().then(function (results) {
     role = results.data;...

In this function in controller I get exception:

TypeError: Cannot read property 'then' of undefined
at n.$scope.login (http://localhost/.../app/controllers/loginController.js:27:30)
at ib.functionCall (http://localhost/.../Scripts/angular.min.js:199:303)
at Ec.(anonymous function).compile.d.on.f (http://localhost/.../Scripts/angular.min.js:216:74)
at n.$get.n.$eval (http://localhost/.../Scripts/angular.min.js:126:15)
at n.$get.n.$apply (http://localhost/.../Scripts/angular.min.js:126:241)
at HTMLButtonElement.<anonymous> (http://localhost/.../Scripts/angular.min.js:216:126)
at HTMLButtonElement.c (http://localhost/.../Scripts/angular.min.js:32:389)

Please help. Thanks.

Upvotes: 2

Views: 5042

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

You need to return promise from function that $http does return itself.

Code

var _getRole = function () {
    return $http.get(serviceBase + 'api/User/CurrentUserRoles');
};

Upvotes: 2

Related Questions