Looking to implement a straightforward registration process using AngularJS. Initially, I retrieve a user with a specific email and assign it to $scope.users. If the method "GetUserByEmail" returns multiple users, I attempt to display a message stating "User already exists". However, a challenge arises. The method GetUserByEmail is being bypassed. The program immediately jumps to the "if" condition and $scope.users remains empty for unknown reasons. Occasionally, after adding a user to the database, the method returns an array of objects and assigns them to $scope.users.
Below is my code featuring the CreateUser method:
var RegisterController = function ($scope, Api, $http) {
$scope.users = {
}
$scope.CreateUser = function () {
var user = {
Password: $scope.password,
Name: $scope.name,
Surname: $scope.surname,
Email: $scope.email,
DateOfBirth: $scope.dateofBirth
}
Api.GetUserByEmail("Users", "GetUserByEmail", $scope.email).then(function (d) {
$scope.users = d;
});
if ($scope.users.length > 0)
{
alert("User already exists!");
$scope.users = {};
}
else
{
Api.PostUser("Users", "PostUser", user).then(function (d) {
alert("Hello");
});
}
};
}
RegisterController.$inject = ['$scope', 'Api', '$http'];
And the GetUserByEmail method:
this.GetUserByEmail = function (controllerName, methodName, email) {
var promise = $http({
method: 'GET',
url: 'api/' + controllerName + '/' + methodName + '?email=' + email,
config: {
params: {
"email": email
}
}
})
.then(function onSuccess(response) {
return response.data;
},
function onError(response) {
return response.statusText;
});
return promise;
}