javascript 类型错误:成功不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30460345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: success is not a function
提问by Mohamed NAOUALI
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() {
}
});
};
})
采纳答案by Mohamed NAOUALI
.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() {
}
});
};
});
回答by koyaanisqatsi
I suspect that error is in this line
我怀疑错误在这一行
service.userExists(account)
service.userExists(帐户)
Your method signature is
你的方法签名是
service.userExists = function(account, success, failure)
service.userExists = 函数(帐户,成功,失败)
so then you call it "success" will be undefined in this case and this line inside "service.userExists"
所以在这种情况下,你称之为“成功”将是未定义的,“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.
这里重写了大约如何避免此错误的示例,并注意更改。我们需要区分失败和用户存在,为了举例,我刚刚将“成功”中的 returnData 更改为 true/false。
.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;
})
})
回答by Pierre Gayvallet
service.userExists = function(account, success, failure)
...
...
if (success (service.userExists(account)))
Calling a 3 method param with only one param.
仅使用一个参数调用 3 方法参数。
回答by messerbill
if (success (service.userExists(account)))
here the script wants to call the function success(param). So you have to define the success() method or you need to check it by another way. it should look like
这里脚本想要调用函数 success(param)。所以你必须定义success()方法或者你需要通过其他方式来检查它。它应该看起来像
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 are calling ->
你真的需要照顾你的呼唤 ->
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"