AngularJs无法读取未定义的属性'then'

yeo*_*uuu 1 angularjs restangular

我有一个包含此功能的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;
    });
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

login() {
    this.authService
        .login(that.user)
        .then(function(user) {
            $scope.setCurrentUser(user);

        },
        function(result) {
            console.log('fail', result.status);
        });
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我在这一行上得到'无法读取属性'然后'未定义': that.authService.login(that.user).then(function (user) ...

use*_*654 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;
    });
}
Run Code Online (Sandbox Code Playgroud)