Greetings, I have been working on validating whether a user has already participated in an event using AngularJS. Here is the relevant code snippet:
HTML
<form>
<span style="display:none">
<input ng-model="userInfo.user_id">
<input ng-model="charity[0].charity_id">
<input ng-model="CurrProjects[0].project_id">
</span>
<button type="submit" class="button button-full button-outline button-assertive" ng-click="check()">
<i class="icon ion-ios-star"></i> I want to volunteer for this! <i class="icon ion-ios-star"></i>
</button>
</a>
</form>
In this example, all three values (user_id, charity_id, project_id) are set to 1.
Here's the controller function that might be causing the issue:
$scope.userVolunteer = {};
$scope.check = function(){
volunteer.checkVol($scope.userInfo.user_id, $scope.charity[0].charity_id, $scope.CurrProjects[0].project_id);
setTimeout(function(){
$scope.checker = volunteer.checkResult();
$scope.$apply();
}, 100);
console.log($scope.checker);
if($scope.checker != null){
$ionicPopup.alert({
title: 'You are already registered to this event'
});
}
else if($scope.checker != []){
$ionicPopup.alert({
title: 'You are already registered to this event'
});
}
else{
console.log("good");
$state.go('app.volunteer');
}
}
Here's the Volunteer service code:
function checkResult(){
return userVolStatus;
}
function checkVol(user, charity, proj){
if(user || charity || proj) {
var userData = {
userId : user,
charId : charity,
projId : proj
}
$http({
method: 'POST',
header: {'Content-Type' : 'application/x-www-form-urlencoded'},
url: 'http://localhost/folder/controller/function',
data: userData
})
.then(
function success( response ) {
userVolStatus = response.data;
console.log(response.data);
},
function error( response ) {
userVolStatus = response.data;
console.log(response.data);
}
);
}
else{
}
return userVolStatus;
}
The problem occurs when there is no data in the database, and it displays "You are already registered to this event!". Any ideas on what could be causing this? Thank you.