【问题标题】:Meteor Autoform package with collection2 does not submit the form带有collection2的Meteor Autoform包不提交表单
【发布时间】:2014-06-03 02:47:34
【问题描述】:

我正在使用 collection2 和 autoform 以及一级嵌套模式

Node = new Meteor.Collection('node',{
schema: new SimpleSchema({
       content: {
           type: String,
           label: "Content",
           max: 200
       }
       created: {
           type: Date,
           label: "Created Date"
       },
       modified: {
           type: Date,
           label: "Modified Date"
       }
   })
});

NodeMeta = new Meteor.Collection('Node_meta',{
schema: new SimpleSchema({
    desc:{
        type:String,
        max:200,
        label:"Description",
        optional:true
    }
})

});

Schema = {};

Schema.nodeWithMeta= new SimpleSchema({
  Node: {
     type: Node.simpleSchema()
  },
  Meta: {
     type: NodeMeta.simpleSchema()
  }
});


{{#autoForm schema=schema id="nodeForm" type="method" meteormethod="nodecreate"}}
  {{> afFieldInput name='Node.content' rows=8 }}
  {{> afFieldInput name='Meta.desc' rows=8}}    
  <button type="submit" class="btn btn-primary btn-submit">Create</button>
{{/autoForm}}

Template.nodeCreate.helpers({
  schema: function() {
    return Schema.nodeWithMeta;
  }
});

它不调用服务器方法。我尝试了 autoform onSubmit 钩子以及 Meteors 内置模板“提交”事件处理程序。如果我使用 jQuery onsubmit 事件处理程序,它会注册该事件。我不能为此目的使用 jQuery,因为 autoform 必须验证输入。

【问题讨论】:

    标签: javascript node.js meteor meteor-blaze


    【解决方案1】:

    您是否允许对您的收藏进行插入/更新?见http://docs.meteor.com/#dataandsecurity

    我敢打赌,如果您运行下面的两个命令,它就会起作用。

    meteor add insecure
    meteor add autopublish 
    

    现在尝试提交。

    如果它确实有效,请关闭自动发布和不安全

    meteor remove insecure
    meteor remove autopublish
    

    然后编写您的允许方法,例如

    Node.allow({
      insert: function (userId, nodeDoc) {
        // write some logic to allow the insert 
        return true;
      }
    });
    

    【讨论】:

      【解决方案2】:

      由于createdmodified 是必填字段,因此很可能没有提交,因为表单中缺少这些字段,这意味着表单无效。实际上有几种不同的方法可以解决这个问题:

      1. 让它们成为可选的(可能不是你想要的)
      2. 为自动表单的 schema 属性使用不同的架构,一个没有这两个字段的架构。
      3. 添加一个“before 方法”挂钩,在提交的文档中设置这两个字段。
      4. 使用autoValue 为这两个字段生成值。

      由于使用autoValue 很容易创建和修改日期,所以我会这样做。像这样的:

      created: {
          type: Date,
          label: "Created Date",
          autoValue: function () {
              if (this.isInsert) {
                return new Date;
              } else {
                this.unset();
              }
          }
      },
      modified: {
          type: Date,
          label: "Modified Date",
          autoValue: function () {
              if (this.isInsert) {
                this.unset();
              } else {
                return new Date;
              }
          }
      }
      

      另外,为了帮助在开发过程中更轻松地解决此类问题,我建议启用debug mode

      【讨论】:

      • 现在我稍微改变了表单提交。我正在使用 onSubmit 而不是表单的 type="method"。我想从客户端插入数据。但由于我的 doc 架构具有嵌套架构,即 doc.Node 和 doc.Meta,它会引发错误code - 第 299 行 SimpleSchema.js> Uncaught: TypeError: Cannot use 'in' operator to search 'pushAll' in undefined。 code
      • 它应该会抛出一个更好的错误,但这意味着它以某种方式调用了 simpleSchema.clean,但为第一个参数传递了undefined。最好在 GitHub 上提交问题,并包含一个指向重现错误的 repo 的链接。
      • 它没有传递未定义,而是直接将我的复杂文档对象传递给它的干净函数。它接受 doc 对象,但由于我的表单架构是由两个合并的架构组成的,因此它在一个 doc 对象中传递 doc.Meta.desc 和 doc.Node.content。
      • 这是意料之中的,因为子模式实际上已被构造函数合并为一个模式。它不应该与它的验证方式有任何区别。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      相关资源
      最近更新 更多