【问题标题】:Meteor AutoForm stops proceeding submitMeteor AutoForm 停止提交
【发布时间】:2016-03-10 21:19:52
【问题描述】:

我想为我的CAS_Entry 集合创建一个表单,使用 Meteor 的 autoform 包。代码如下所示。我还添加了定义的钩子,不幸的是,其中只有beginSubmitbefore 被执行并且没有条目被添加到集合中。使用 Meteor shell,插入就像一个魅力。

感谢任何提示。

addCasEntry.html,表单展示模板:

{{#autoForm collection="CAS_Entry" type="insert" id="addCasEntryForm"}}
  {{> afQuickField name="type" options="allowed"}}
  {{> afQuickField name="description" rows="6" type="textarea"}}
  {{> afQuickField name="file" type="cfs-file" collection="Images"}}
  {{> afQuickField name="date" }}
  <button type="submit" class="btn btn-primary">Add</button>
{{/autoForm}}

addCasEntry.js,添加调试钩子:

AutoForm.hooks({
  addCasEntryForm: {
    before: {
      insert: function(doc) {
        console.log(doc);
      }
    },
    after: {
      insert: function(error, result) {
        console.log('Occured error: ' + error);
      }
    },
    beginSubmit: function() {
      console.log('begin submit');  
    },
    onSuccess: function(formType, result) {
      console.log("Insert succeeded");
      console.log('Result ' + result);
    },
    onError: function(formType, error) {
      console.log('Error!!!');
      console.log(error);
    }
  }
});

SimpleSchema.debug = true;

/lib/collection/cas_entry.js:

CAS_Entry = new Mongo.Collection("cas_entries");

CAS_Entry.attachSchema(new SimpleSchema({
  type: {
    type: String,
    allowedValues: ['reflection', 'evidence']
  },
  description: {
    type: String,
    optional: true
  },
  file: {
    type: String,
    optional: true,
  },
  timeUploaded: {
    type: Date,
    optional: true,
    autoValue: function() {
      return new Date();
    }
  },
  date: {
    type: Date,
  }
}));

CAS_Entry.allow({
  'insert': function() {
    return true;
  },
  'update': function() {
    return true;
  }
});

这是控制台输出:

【问题讨论】:

  • 您是否在表单中附加了架构?
  • 我为集合附加了一个模式。因此,据我所知,autoform 应该默认使用它。这适用于我的其他表格。
  • 是的,这就是我的意思。那么您是否尝试过调试选项来查看架构是否可能会过滤掉您的一些数据?
  • SimpleSchema.debug = true

标签: javascript meteor meteor-autoform meteor-collection2


【解决方案1】:

您的表单将不会被提交,因为您没有将文档返回或传递给您的 before 挂钩中的 this.result();

AutoForm.hooks({
  addCasEntryForm: {
    // ...
    before: {
      insert: function(doc) {
        console.log(doc);
        return doc;
      }
    }
    // ...
  }
});

根据documentation,您应该根据您定义的前提条件使用以下语句之一:

  • 同步,提交:return doc;
  • 同步,取消:return false;
  • 异步,提交:this.result(doc);
  • 异步,取消:this.result(false);

【讨论】:

  • 好吧,非常愚蠢的错误,我不得不承认。所以它是程序员开始研究代码时出现的一个bug。
猜你喜欢
  • 2016-12-04
  • 2015-09-15
  • 2014-06-03
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 2015-12-01
  • 2015-07-06
  • 1970-01-01
相关资源
最近更新 更多