2

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.

10
  • have you injected $http ? Commented May 2, 2015 at 18:14
  • is service - of course, in controller - no Commented May 2, 2015 at 18:15
  • 1
    ok, now - you are not returning the promise from _getRole Commented May 2, 2015 at 18:18
  • 1
    @c69 duplicate answer is telling to create custom promise & returning that custom promise..I would not prefer that way...you could look at my answer..that is reusing promise return by $http Commented May 2, 2015 at 18:30
  • 1
    .then(function (results) { return results; }); looks totally superfluous? Remove it. Commented May 2, 2015 at 18:37

1 Answer 1

2

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

Code

var _getRole = function () {
    return $http.get(serviceBase + 'api/User/CurrentUserRoles');
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.