【发布时间】:2014-01-27 03:00:52
【问题描述】:
我正在使用 Collection2 包为我的 Meteor 应用程序集成架构和简单的验证方法。
插入工作正常,会适当地出错,并且在按预期有效时插入。根据文档,使用更新方法几乎相同,但验证错误不断出现,即使每个架构都有效。
因此,例如,我将提交一个新的属性(一个实际的住宅/公寓),它根据架构进行验证,它没有问题。我将去编辑该属性,在 1 个字段上更改一个字母,然后在各个字段上出错。
我有点不知所措。这很简单。在介绍 Collection2 之前,我更新文档没有问题,但我认为包本身没有问题,因为我知道它正在使用并正在积极更新。也许我需要把这个方法放在服务器上?
任何帮助将不胜感激。谢谢!
客户端JS文件:
Template.propertyEdit.events({
'submit form': function(e){
e.preventDefault();
var property_details = {
name: $(e.target).find('[name=name]').val(),
address: $(e.target).find('[name=address]').val(),
city: $(e.target).find('[name=city]').val(),
state: $(e.target).find('[name=state]').val(),
zipcode: $(e.target).find('[name=zipcode]')
}
Properties.update(this._id, {$set: property_details}, function(error,result){
if(error){
for(var i=0; Properties.simpleSchema().namedContext().invalidKeys().length > i; i++ ){
throwError(Properties.simpleSchema().namedContext().invalidKeys()[i].message);
}
}else{
alert("Property details updated");
Router.go('propertyOverview', {_id: result});
}
});
});
收藏:
Properties = new Meteor.Collection2('properties', {
schema: {
name: {
type: String,
label: "Property Name",
min: 1
},
address: {
type: String,
label: "Address",
min: 1
},
city: {
type: String,
label: "City",
min: 1
},
state: {
type: String,
label: "State",
min: 2,
max: 2
},
zipcode: {
type: String,
label: "Zip Code",
min: 5,
max: 11
},
userId: {
type: String,
label: "User Id",
min: 8
}
}
});
【问题讨论】:
-
您的示例似乎缺少
.val(),例如$(e.target).find('[name=name]').val() -
谢谢内森,错字。尽管我的粘贴工作很糟糕,但错误仍然存在。
-
附带问题:为什么不使用 AutoForm?
-
当我读到它时,我已经有一些东西正在“进行”。我肯定会在以后集成它。
标签: javascript validation collections meteor meteor-collection2