我在_id字段上使用自定义验证,如下所示。首先,我输入一个ID并点击提交,它将保留在数据库中。下次,我重用相同的ID并尝试提交,我收到验证错误。我再次点击提交按钮,我在chrome日志中抛出一个奇怪的内部服务器错误。验证错误消失并感觉好像表单已成功提交,但实际上并非如此。我无法弄清楚为什么会在第二次尝试时发生这种情况。通常,验证错误必须一次又一次地出现,我点击提交的次数。我做错了什么?
架构:
id":{
type:String,
label: "Template Name",
unique: true,
autoform:{
textHelp:"Unique name to identify the template"
},
custom: function() {
if (Meteor.isClient && this.isSet) {
Meteor.call("isAggrUnique", this.value, function(error, result) {
if (!result) {
aggrRouter_aaaContext_template_collection.simpleSchema().namedContext("aggrRouter_aaaContext_template_form").addInvalidKeys([{
name: "_id",
type: "notUnique"
}]);
}
});
}
if (Meteor.isServer) {
Meteor.methods({
isAggrUnique: function(id) {
return aggrRouter_aaaContext_template_collection.find({
_id: id.toUpperCase()
}).count() === 0;
}
});
}
}
},
多次提交后,Chrome开发日志会显示:
aldeed_autoform.js?hash=9676200…:7782 errorClass {error: 500, reason: "Internal server error", details: undefined, message: "Internal server error [500]", errorType: "Meteor.Error"}
Meteor服务器日志显示:
I20160720-17:23:11.392(5.5)? Exception while invoking method '/aggrRouter_aaaContext_template_collection/insert' Error: A method named 'isAggrUnique' is already defined
I20160720-17:23:11.393(5.5)? at packages/ddp-server/livedata_server.js:1548:15
I20160720-17:23:11.393(5.5)? at Function._.each._.forEach (packages/underscore/underscore.js:113:1)
I20160720-17:23:11.393(5.5)? at Server.methods (packages/ddp-server/livedata_server.js:1544:7)
I20160720-17:23:11.394(5.5)? at Object.custom (both/collections/aaa_context_schema.js:23:24)
I20160720-17:23:11.394(5.5)? at packages/aldeed_simple-schema/simple-schema-validation.js:170:1
I20160720-17:23:11.394(5.5)? at packages/underscore/underscore.js:221:1
I20160720-17:23:11.394(5.5)? at _.each._.forEach (packages/underscore/underscore.js:108:1)
I20160720-17:23:11.395(5.5)? at Function._.every._.all (packages/underscore/underscore.js:220:1)
I20160720-17:23:11.395(5.5)? at validate (packages/aldeed_simple-schema/simple-schema-validation.js:169:1)
I20160720-17:23:11.395(5.5)? at checkObj (packages/aldeed_simple-schema/simple-schema-validation.js:227:1)
为什么说我不止一次定义isAggrUnique
?