【问题标题】:Meteor Collection2 Update throwing errorsMeteor Collection2 更新抛出错误
【发布时间】: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


【解决方案1】:

终于看到了问题..在模板中,HTML,占位符属性似乎注册为一个值,即使值属性有内容..

我有:

<input name="zipcode" type="text" class="form-control" value="{{zipcode}}" placeholder="Enter a Zip Code" />

改成:

<input name="zipcode" type="text" class="form-control" value="{{zipcode}}" />

它成功了:)

不确定这是刚刚知道的,我应该知道的,还是一个错误。无论如何我都会提交。

【讨论】:

  • 嗯。对于没有 html5 的浏览器,一些插件会将 placeholder 复制到 value 属性中。
猜你喜欢
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 2016-03-28
  • 2014-01-28
  • 2014-12-31
  • 2015-03-27
  • 1970-01-01
相关资源
最近更新 更多