我有一个包含此功能的authenticationService:
//this.userApi = Restangular.service('api/sessions');
login(user) {
let that = this;
this.userApi.post(user).then(function(user) {
that.session.create(user.sessionId, user.userId, user.username);
return user;
});
}
在我的控制器中:
login() {
this.authService
.login(that.user)
.then(function(user) {
$scope.setCurrentUser(user);
},
function(result) {
console.log('fail', result.status);
});
}
我不明白为什么我会得到“无法阅读财产”然后'未定义的'在这一行:that.authService.login(that.user).then(function (user) ...
答案 0 :(得分:7)
在您的第一个代码段中,您需要返回承诺,以便您可以在其他代码段中继续链接。
//this.userApi = Restangular.service('api/sessions');
login(user) {
let that = this;
return this.userApi.post(user).then(function(user) {
that.session.create(user.sessionId, user.userId, user.username);
return user;
});
}
答案 1 :(得分:4)
你应该试试这个:
//this.userApi = Restangular.service('api/sessions');
login(user) {
let that = this;
return this.userApi.post(user).then(function(user) {
that.session.create(user.sessionId, user.userId, user.username);
return user;
});
}