【发布时间】: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);”谢谢
-
Looking into the restangular library for setBaseUrl 看起来他们使用它的方式不同。这有关系吗?看来您需要先使用configurer
-
根据github.com/mgonto/restangular/blob/…,我可以直接将此功能与Restangular一起使用。我试图 log.info() 我的 $Session 对象,它包含我工厂中定义的所有函数。但是用 Restangular 做同样的事情会返回“20”而不是一个有函数的对象......不明白。
标签: javascript angularjs restangular