I am trying synchronize two call backs one from service and one directly called . (This is old code so please ignore code quality)
if($scope.newUser.password == $scope.newUser.password2)
{
// search for the user to see if it already exists
UserValidateService.findUserByEmail($scope.newUser.username, $scope.newUser.email, function(user){
console.log(user);
if(user[0]) {
alert('email Id already exists ******');
return false;
}
}).then(function(){
$http.get("/api/user/"+$scope.newUser.username)
.success(function(newUser) {
// if user does not exist, create it new
if(newUser.length == 0) {
$http.post("/api/user", $scope.newUser)
.success(function(newUser){
if(newUser == null)
alert("Unable to register user");
else
$location.path( $scope.newUser.username+"/home" );
});
}
else
{
alert("User already exists");
}
});
});
}
else
{
alert("Passwords must match. Try again");
}
Service :
app.factory('UserValidateService', function($http){
var findUserByEmail = function (user, email, callback) {
$http.get("/api/user/"+user+"/email/"+email)
.success(callback);
};
return {
findUserByEmail: findUserByEmail
};
});
Error:
TypeError: Cannot read property 'then' of undefined
at h.$scope.register (user.js:118)
at angular.js:10567
at angular.js:18627
at h.$eval (angular.js:12412)
at h.$apply (angular.js:12510)
at HTMLButtonElement.<anonymous> (angular.js:18626)
at HTMLButtonElement.jQuery.event.dispatch (jquery.js:5095)
at HTMLButtonElement.elemData.handle (jquery.js:4766)
Can any please let me where I am going wrong or how to make this code synchronize i.e calling