TypeError: success is not a function

Can someone help me to get this fixed. Actually I am trying to register a new user and before creating a new one I have to check if the username exists or not. below is using factory.

.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() {
                        }
                    });
                };
            })

      

+3


source to share


4 answers


.factory(
            'accountService',
            function($resource, sessionService) {
                var service = {};
                service.register = function(account, success, failure) {
                    service
                            .validUserName(
                                    account,
                                    function(answer) {
                                        if (answer) {
                                            console.log(answer);
                                            failure();
                                        } 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() {
                        console.log("user details : ", data);
                        var accounts = data.username;
                        if (accounts && accounts.length !== 0) {
                            service.data = data;
                            success(account);
                        } else {
                            failure();
                        }
                    }, failure);
                };
                service.validUserName = function(account, success, failure) {
                    var Account = $resource("/Test/rest/account/user");
                    var data = Account.get({
                        username : account.username
                    }, 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;
            })

      

and for the controller:



.controller(
            "RegisterController",
            function($scope, sessionService, $state, accountService) {
                $scope.register = function() {
                    accountService.register($scope.account, function(
                            returnedata) {
                        accountService.userExists($scope.account, function(
                                account) {
                            sessionService.login($scope.account).then(
                                    function() {
                                        $state.go("get_started");
                                    });
                        });

                    }, function() {
                        $scope.registererror = function() {
                        }
                    });
                };
            });

      

0


source


I suspect the error is on this line

service.userExists (accounts)

Your method signature

service.userExists = function (account, success, failure)

then you call it "success" will be undefined in this case and this line inside "service.userExists"



Success (bills)

gives an error message

Here is an example of roughly how to avoid this error, change notes. We need to distinguish between failure and user existence, I just changed returnData from "success" to true / false for the 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;

      

})

+3


source


service.userExists = function(account, success, failure)

      

...

if (success (service.userExists(account)))

      

Method parameter call 3 with only one parameter.

0


source


 if (success (service.userExists(account)))

      

here the script wants to call the success (param) function. Therefore, you need to define the success () method, or you need to test it in a different way. it should look like

if(service.userExists(account)){ 
  failure(); //also needs to be defined!
} else{
  var Account = $resource("/Test/rest/account/student");
  Account.save({}, account, success, failure);
}

      

you really need to take care of what you call →

var test = "hello";
console.log(test); //-> prints "hello"

var test = function(){
  console.log("hello")
}
console.log(test); //-> prints sth. like "function()....."
console.log(test()); //-> prints "hello"

      

0


source







All Articles