Can somebody help me to get this fixed. In fact I'm trying to register a new user and before creating a new one I have to check if the username exists or not. below is the factory used for that.
.factory('accountService', function($resource, sessionService) {
var service = {};
service.register = function(account, success, failure) {
if (success (service.userExists(account))){
failure();
} else {
var Account = $resource("/Test/rest/account/student");
Account.save({}, account, success, failure);
}
};
service.userExists = function(account, success, failure) {
var Account = $resource("/Test/rest/account");
var data = Account.get({
username : account.username,
password : account.password
}, function() {
var accounts = data.username;
if (accounts && accounts.length !== 0) {
service.data = data;
success(account);
} else {
failure();
}
}, failure);
};
service.getuser = function() {
return service.data;
};
return service;
})
but when I run it, i get this error message:
TypeError: success is not a function
below is the controller that uses this factory
.controller(
"RegisterController",
function($scope, sessionService, $state, accountService) {
$scope.register = function() {
accountService.register($scope.account, function(
returnedata) {
sessionService.login($scope.account).then(
function() {
$state.go("home");
});
}, function() {
$scope.registererror = function() {
}
});
};
})
I suspect that error is in this line
service.userExists(account)
Your method signature is
service.userExists = function(account, success, failure)
so then you call it "success" will be undefined in this case and this line inside "service.userExists"
success(account)
throws a error
Here is rewritten approximately example of how to avoid this error, notes about changes. We need to distinguish between failure and user existence, I've just changed returnData in "success" to true/false for the sake of an example.
.factory('accountService', function($resource, sessionService) {
var service = {};
service.register = function(account, success, failure) {
service.userExists(account), function(answer){
if (answer){
success();
} else {
var Account = $resource("/Test/rest/account/student");
Account.save({}, account, success, failure);
}
}, failure);
};
service.userExists = function(account, success, failure) {
var Account = $resource("/Test/rest/account");
var data = Account.get({
username : account.username,
password : account.password
}, function() {
var accounts = data.username;
if (accounts && accounts.length !== 0) {
service.data = data;
success(true);
} else {
success(false);
}
}, failure);
};
service.getuser = function() {
return service.data;
};
return service;
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With