在返回promise的函数上使用.success时,Undefined不是函数
我正在尝试通过编写登录功能来继续我对诺言的理解.登录功能是AuthService的一部分,该服务将http请求发送到我的服务器并返回一个Promise(如果已验证用户,则Promise将被解决;否则,将被拒绝).
I am trying to continue my understanding of promises by writing a login function. The Login function is part of an AuthService that sends an http request to my server and returns a promise (the promise is resolved if the user is verified and rejected if not).
在控制器中调用该函数时,由于该函数返回一个Promise,但我尝试使用.success和.error,但出现错误"Undefined is not a function".
When calling the function in my controller I try to use .success and .error since the function returns a promise but I get the error "Undefined is not a function".
这是我的函数,得到未定义的错误.
Here is my function that gets the undefined error.
$scope.login = function(email, password) {
return AuthService.login(email, password).success(function(data) {
return console.log("login function returned", data);
}).error((function(data) {
return console.log("There was an error " + data);
}));
};
这是我的服务
.service('AuthService', function($window, $rootScope, $q, $http) {
return {
login: function(email, password) {
var deferred;
deferred = $q.defer();
$http.post('/dashboard/login', {
email: email,
password: password
}).error(function(error, status) {
console.log("Incorrect logi");
return deferred.reject(error);
}).success(function(data, status) {
$window.sessionStorage["userInfo"] = JSON.stringify(data);
return deferred.resolve(data);
});
return deferred.promise;
}
};
});
最让我困惑的是,经过研究,promise似乎没有成功或错误的方法,但是$ http是具有该方法的promise.另外,如果他们没有成功和错误,您怎么知道某事是否是错误?最后,如果没有成功或错误方法,.reject和.resolve有什么意义?
What confuses me most is that after researching it seems like promises don't have a success or error method but $http is a promise that has that method. Also if they don't have success and error how do you know if something is an error or not? Lastly what is the point of .reject and .resolve if there is no success or error method?
最让我困惑的是,经过研究,promise似乎没有成功或错误的方法,但是$ http是具有该方法的promise
What confuses me most is that after researching it seems like promises don't have a success or error method but $http is a promise that has that method
是的,的确如此. $ http
返回对象是也出于遗留原因而具有这些 .success
和 .error
方法的保证.>
Yes, indeed. $http
return objects are promises that also have these .success
and .error
methods for legacy reasons.
如果他们没有成功和错误,怎么知道是不是错误?
if they don't have success and error how do you know if something is an error or not?
The standard way to attach handlers to a promise is using the .then()
method, which also acts as a chaining method and returns a new promise for the results of the handlers.
所以代码看起来像这样(也是避免使用延迟的反模式):
So the code would look like this (also avoiding the deferred antipattern):
.service('AuthService', function($window, $rootScope, $q, $http) {
return {
login: function(email, password) {
return $http.post('/dashboard/login', {
email: email,
password: password
}).then(function(data, status) {
$window.sessionStorage["userInfo"] = JSON.stringify(data);
return data;
}, function(error, status) {
console.log("Incorrect login");
throw error;
});
}
};
});
$scope.login = function(email, password) {
return AuthService.login(email, password).then(function(data) {
return console.log("login function returned", data);
}, function(err) {
return console.log("There was an error " + err);
});
};