Answer the question
In order to leave comments, you need to log in
Using a service in a then() controller?
Hello. I can't figure it out.
There is an auth controller and there is a service
. I send the form to the server and get successful data, etc. But swears TypeError: Cannot read property 'then' of undefined.
Controller code
$scope.login = function(){
authService.login($rootScope.user).then(
function(response){
if(response.data.state === 'success'){
$cookies.putObject('user', response.data.user, {expires: expireDate});
$rootScope.authenticated = true;
$rootScope.current_user = response.data.user.username;
$location.path('/');
}
else{
$rootScope.error_message = response.data.message;
}
},
function(response){
$rootScope.error_message = "Some error with register" + response.data.status;
}
);
};
authService.login = function(data){
$http.post('/auth/login', data).then(function(response){
return response;
}, function(response){
});
};
Answer the question
In order to leave comments, you need to log in
You need to do a return:
return $http.post('/auth/login', data).then(function(response){ ...
That's how it worked
authService.login = function(data){
var deferred = $q.defer();
$http.post('/auth/login', data).then(function(response){
setTimeout(function() {
if(response.data){
deferred.resolve(response);
}else {
deferred.reject('err');
}
},1000);
});
return deferred.promise;
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question