0

I'm trying to play with angularjs and restangular. I have problems using restangular as it give me an error:

TypeError: undefined is not a function
        at Object.login (localhost:8000/src/common/factories/session.js:110:38)
        at localhost:8000/src/common/factories/session.js:207:34
        at Scope.$emit (localhost:8000/vendor/angular/angular.js:12809:33)
        at Scope.$scope.Login (localhost:8000/src/common/factories/session.js:185:28)
        at localhost8000/vendor/angular/angular.js:10735:21
        at localhost:8000/vendor/angular/angular.js:18942:17
        at Scope.$eval (localhost:8000/vendor/angular/angular.js:12595:28)
        at Scope.$apply (localhost:8000/vendor/angular/angular.js:12693:23)
        at Scope.$delegate.__proto__.$apply (<anonymous>:855:30)
        at HTMLButtonElement.<anonymous> 

(localhost:8000/vendor/angular/angular.js:18941:21)

My controller is here : https://github.com/Seraf/LISA-WEB-Frontend/blob/master/src/common/factories/session.js#L188 and calling the login function (line and pos in the error above)

Here, it seems it doesn't play well with Restangular as I have the error pasted above. As I'm new to angularjs, maybe I don't do the best practices ... I already inject restangular as dependency on my root module : https://github.com/Seraf/LISA-WEB-Frontend/blob/master/src/app/app.js#L6 maybe it causes some problems ?

[EDIT] Guilty lines are :

110: ".setBaseUrl('backend/api/v1')"

207: "$Session.login(data);"

The guilty code :

angular.module("SessionManager", ['http-auth-interceptor','restangular'])
        .constant('constSessionExpiry', 20) // in minutes
        .factory("$Session", [
                '$rootScope',
                '$q',
                '$location',
                '$log',
                '$http',
                'constSessionExpiry',


                function($rootScope, $q, $location, $log, $http, Restangular, authService, constSessionExpiry) {
                    return {
                        login: function(data){
                                $log.info("Preparing Login Data", data);
                                var $this = this;
                                return Restangular
                                    .setBaseUrl('backend/api/v1')
                                    .all('user/login/')
                                    .post(data)
                                    .then(function userLoginSuccess(response){
                                            $log.info("login.post: auth-success", response);
                                            $this.User = response;
                                            // remove properties we don't need.
                                            delete $this.User.route;
                                            delete $this.User.restangularCollection;
                                            $this.User.is_authenticated = true;
                                            $this.cacheUser();
                                            $this.setApiKeyAuthHeader();
                                            $this.authSuccess();
                                        }, function userLoginFailed(response){
                                            $log.info('login.post: auth-failed', response);
                                            $this.logout();
                                            return $q.reject(response);
                                        });
                        },
                    };
                }])

Thanks for the help !

5
  • It's hard to tell where these errors are when the line numbers in the error don't seem to match the javascript file. Would you confirm what lines 110 and 207 of session.js say? Commented Jul 11, 2014 at 13:19
  • You're right, I did a modification since the error paste, sorry. The bad line at 110 is : ".setBaseUrl('backend/api/v1')" And the 207 is :"$Session.login(data);" Thanks Commented Jul 11, 2014 at 13:39
  • The error means that $Session is undefined at L221, at this point I can't be much help since I've never used the angular library, but it looks like when function($rootScope, $log, $Session, $state){ is called, that it is not receiving a $Session parameter. L208 Commented Jul 11, 2014 at 13:57
  • Looking into the restangular library for setBaseUrl it doesn't look like they use it the same way. Is that related? It looks like you need to use a configurer first Commented Jul 11, 2014 at 14:15
  • According to github.com/mgonto/restangular/blob/… I can use this function with Restangular directly. I tried to log.info() my $Session object, and it contains all functions defined in my factory. But trying the same thing with Restangular returns me "20" and not an object with functions... Don't understand. Commented Jul 11, 2014 at 14:24

1 Answer 1

2

You've got your dependency injection wrong:

.factory("$Session", [
         '$rootScope','$q','$location','$log','$http','constSessionExpiry',
 function($rootScope, $q, $location, $log, $http, Restangular, authService, constSessionExpiry) {

You're injecting constSessionExpiry as Restangular and nothing for the latter two services.

This means that calling Restangular.setBaseUrl is actually looking for 20.setBaseUrl which is undefined and not a function -- hence the error!

To get on the road to a fix, make sure you're passing the right service names to match your function arguments, e.g.:

.factory("$Session", [
         '$rootScope','$q','$location','$log','$http','Restangular','authService','constSessionExpiry',
 function($rootScope, $q, $location, $log, $http, Restangular, authService, constSessionExpiry) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, it solved my problem. I didn't understood that dependencies order have an effect on function arguments. Sorry, I need to read more documentation about angularjs.

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.