【问题标题】:How to add a required dateSubmitted on the server with Meteor autoform?如何使用 Meteor autoform 在服务器上添加所需的 dateSubmitted?
【发布时间】:2015-08-03 14:03:50
【问题描述】:

我有一个帖子集合,需要dateSubmitted 字段。但是,此字段的值应使用服务器时间在服务器上生成。

Posts.attachSchema(new SimpleSchema({
  title: {
    type: String,
    optional: false
  },
  content: {
    type: String,
    optional: false
  },
  dateSubmitted: {
    type: Date,
    optional: false
  }
}));

问题在于,如果我只有titlecontent 的值,则表单不会在客户端上提交。但如果没有从服务器生成的 dateSubmitted 值,我也无法将其插入集合中。

我查看了 Autoform 钩子,但在这种情况下似乎没有任何效果。

这是我想要的工作流程:

  1. Schema 表示 titlecontentdateSubmitted 是必需的。
  2. 客户端仅使用titlecontent成功提交表单。
  3. 服务器添加dateSubmitted
  4. 然后新文档被插入到集合中。 4.a.如果由于某种原因无法生成 dateSubmitted,则会将错误发送回客户端。

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    假设您已经在使用aldeed:collection2,那么在您的架构中使用以下内容:

    dateSubmitted: {
        type: Date,
        autoValue: function(){
          if ( this.isInsert ) return new Date();
          else if ( this.isUpsert ) return { $setOnInsert: new Date };
          else if ( this.isSet ) this.unset();
        }
      }
    

    这将自动设置插入/更新插入的日期,然后拒绝所有以后的更改。

    来自http://0rocketscience.blogspot.com/2015/07/meteor-security-no-2-all-praise-aldeed.html

    【讨论】:

    • 谢谢,已更正。 CamelCase 还是 camelCase,这一直是个问题......
    猜你喜欢
    • 1970-01-01
    • 2017-04-21
    • 2023-03-29
    • 2015-05-08
    • 2017-10-05
    • 2015-09-22
    • 1970-01-01
    • 2020-09-26
    • 2015-05-11
    相关资源
    最近更新 更多