[英]Why am I geting TypeError: onSuccess is not a function in my angularjs service
I have an angularjs services that leverages restangular to make calls. 我有一个利用restangular进行调用的angularjs服务。
angular.module('app.userSvc', [])
.factory('userSvc', ['localStorageService', '$rootScope', 'Restangular',
function(localStorageService, $rootScope, Restangular) {
function checkIfLoggedIn() {
return localStorage.getItem('token');
}
function login(email, password, onSuccess, onError) {
Restangular.all('api/authenticate')
.post({
email: email,
password: password
})
.then(
function(response) {
localStorage.setItem('token', response.token);
onSuccess(response);
},
function(response) {
onError(response);
});
}
function logout() {
localStorageService.remove('token');
}
function getCurrentToken() {
return localStorage.getItem('token');
}
function getAuthenticatedUser(onSuccess, onError) {
Restangular.one('api/authenticated_user?token=' + getCurrentToken()).get().then(function(response) {
onSuccess(response.user);
}, function(response) {
onError(response);
});
}
Restangular.setDefaultHeaders({
'Authorization': 'Bearer ' + getCurrentToken()
});
return {
checkIfLoggedIn: checkIfLoggedIn,
login: login,
logout: logout,
getCurrentToken: getCurrentToken,
getAuthenticatedUser: getAuthenticatedUser
};
}
]);
But when i call the userSvc.getAuthenticatedUser() method in a controller i get TypeError: onSuccess is not a function This is how am calling it. 但是,当我在控制器中调用userSvc.getAuthenticatedUser()方法时,我得到TypeError:onSuccess不是函数,这就是调用它的方式。
console.log(userSvc.getAuthenticatedUser());
What am i doing wrong. 我究竟做错了什么。 I am using angular 1.6
我正在使用角度1.6
You HAVE to set a callback function as the first argument, because inside getAuthenticatedUser()
, there is no check to see whether the onSuccess
argument is set or not. 您必须将回调函数设置为第一个参数,因为在
getAuthenticatedUser()
,无法检查是否设置了onSuccess
参数。 (The same is actually true for the onError callback, so you need to provide that second argument, too) (对于onError回调实际上也是如此,因此您也需要提供第二个参数)
So, either implement such a check (eg onSuccess && onSuccess()
) inside the getAuthenticatedUser
function, or provide a callback function (even if it's an anonymous one that doesn't to anything) in order to prevent that error. 因此,可以在
getAuthenticatedUser
函数内部实现这样的检查(例如onSuccess && onSuccess()
),或者提供一个回调函数(即使它是匿名的,不做任何事情)以防止该错误。
userSvc.getAuthenticatedUser(function() {}, function() {});
Error is expected, As your service method expected callback method so pass them 预计会出现错误,因为您的服务方法应使用回调方法,所以请传递
function getAuthenticatedUser(onSuccess, onError){
}
So you need to pass the callback methods 所以你需要传递回调方法
userSvc.getAuthenticatedUser(function() {}, function() {})
Or, You can check whether argument is defined and a angular.isFunction 或者,您可以检查是否定义了参数和angular.isFunction。
function getAuthenticatedUser(onSuccess, onError) {
Restangular.one('api/authenticated_user?token=' + getCurrentToken()).get().then(function (response) {
!!onSuccess && angular.isFunction(onSuccess) && onSuccess(response.user);
}, function (response) {
!!onError && angular.isFunction(onError) &&
onError(response);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.