Restangular:TypeError:undefined不是函数

时间:2014-07-11 12:57:44

标签: javascript angularjs restangular

我试图使用angularjs和restangular。 我有问题使用restangular,因为它给我一个错误:

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)

我的控制器在这里:https://github.com/Seraf/LISA-WEB-Frontend/blob/master/src/common/factories/session.js#L188并调用登录功能(上面错误中的行和位置)

在这里,似乎它与Restangular没有很好的配合,因为我上面粘贴了错误。 由于我是angularjs的新手,也许我没有做最好的做法...我已经注入了restangular作为对我的根模块的依赖:https://github.com/Seraf/LISA-WEB-Frontend/blob/master/src/app/app.js#L6可能会导致一些问题?

[编辑] 有罪的行是:

110:&#34; .setBaseUrl(&#39; backend / api / v1&#39;)&#34;

207:&#34; $ Session.login(data);&#34;

有罪的代码:

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);
                                        });
                        },
                    };
                }])

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您的依赖注入错误:

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

您为constSessionExpiry注入了Restangular而后两项服务没有注入任何内容。

这意味着调用Restangular.setBaseUrl实际上是在寻找未定义的20.setBaseUrl而不是函数 - 因此错误!

要开始修复,请确保您传递正确的服务名称以匹配您的函数参数,例如:

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