【问题标题】:Restangular: TypeError: undefined is not a functionRestangular:TypeError:未定义不是函数
【发布时间】:2014-07-12 05:43:33
【问题描述】:

我正在尝试使用 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 并调用登录函数(上面错误中的 line 和 pos)

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

[编辑] 有罪的台词是:

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

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

有罪代码:

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

感谢您的帮助!

【问题讨论】:

  • 当错误中的行号似乎与 javascript 文件不匹配时,很难判断这些错误在哪里。你能确认一下 session.js 的第 110 和 207 行是什么意思吗?
  • 你说得对,自从错误粘贴后我做了修改,对不起。 110 处的坏行是:“.setBaseUrl('backend/api/v1')” 而 207 是:“$Session.login(data);”谢谢
  • 错误意味着$SessionL221处未定义,此时我帮不上什么忙,因为我从未使用过角度库,但看起来像function($rootScope, $log, $Session, $state){被调用,它没有接收到 $Session 参数。 L208
  • Looking into the restangular library for setBaseUrl 看起来他们使用它的方式不同。这有关系吗?看来您需要先使用configurer
  • 根据github.com/mgonto/restangular/blob/…,我可以直接将此功能与Restangular一起使用。我试图 log.info() 我的 $Session 对象,它包含我工厂中定义的所有函数。但是用 Restangular 做同样的事情会返回“20”而不是一个有函数的对象......不明白。

标签: javascript angularjs restangular


【解决方案1】:

你的依赖注入错误:

.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) {

【讨论】:

  • 非常感谢,它解决了我的问题。我不明白依赖顺序对函数参数有影响。抱歉,我需要阅读更多关于 angularjs 的文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 2016-10-04
  • 2015-06-27
  • 2018-05-14
  • 2015-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多