有人可以帮我解决这个问题。 事实上,我正在尝试注册新用户,在创建新用户之前,我必须检查用户名是否存在。以下是用于此的工厂。
.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;
})
但是当我运行它时,我收到此错误消息:
下面的TypeError:成功不是函数
是使用此工厂的控制器
.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() {
}
});
};
})
答案 0 :(得分:3)
我怀疑错误在这一行
service.userExists(帐户)
您的方法签名是
service.userExists = function(account,success,failure)
所以你称之为"成功"在这种情况下将是未定义的 这条线在" service.userExists"
中成功(帐户)
抛出错误
这里重写了大致如何避免此错误的示例,有关更改的说明。我们需要区分失败和用户存在,我只是在"成功"中更改了returnData。为了一个例子,为了真/假。
.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;
})
答案 1 :(得分:0)
service.userExists = function(account, success, failure)
...
if (success (service.userExists(account)))
只用一个参数调用3方法参数。
答案 2 :(得分:0)
if (success (service.userExists(account)))
这里脚本想要调用函数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);
}
你真的需要照顾你所说的 - >
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"
答案 3 :(得分:0)
.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;
})
和控制器:
.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() {
}
});
};
});