Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor simpleschema验证和插入前收集挂钩_Meteor - Fatal编程技术网

Meteor simpleschema验证和插入前收集挂钩

Meteor simpleschema验证和插入前收集挂钩,meteor,Meteor,使用before.insert收集挂钩时,如下所示: collection.before.insert(function(userId, doc) { doc.createdAt = Date.now(); }); 如果createdAt是一个必填字段(用SimpleSchema声明),那么由于在插入钩子之前进行了验证,服务器将触发一个“Error:createdAt是必需的”异常 插入钩子发生后如何验证架构?将Collection2添加到项目中(meteor Add-aldeed:Col

使用before.insert收集挂钩时,如下所示:

collection.before.insert(function(userId, doc) {
  doc.createdAt = Date.now();
});
如果createdAt是一个必填字段(用SimpleSchema声明),那么由于在插入钩子之前进行了验证,服务器将触发一个“Error:createdAt是必需的”异常


插入钩子发生后如何验证架构?

将Collection2添加到项目中(
meteor Add-aldeed:Collection2
假设尚未添加),并使用
autoValue
。在客户端上运行AutoValue操作仅用于验证,但无论插入/更新是从客户端还是从服务器启动,保存的实际值始终会在服务器上生成


您的另一个选择是将字段设置为可选,并使用钩子,但这并不理想。

不幸的是,我不确定,但考虑到验证通常用于用户输入,这可能与此无关。只需从模式定义中删除createdAt,因为钩子将保证数据正确。SimpleSchema验证不仅仅用于用户输入。您可以有一个必填字段(作为“userId”),但不是在用户输入时,而是在文档插入时(用户在填写表单时不需要输入自己的ID,服务器端负责添加userId)。我同意钩子将保证正确的数据,我只是不满意:)我不知道autovalue。但这与我想做的相反。before.insert收集钩子位于我正在编写的包中,该包负责自动字段插入()。我无法在架构定义中进行插入。
createdAt: {
 type: Date,
  autoValue: function() {
    if (this.isInsert) {
      return new Date;
    } else if (this.isUpsert) {
      return {$setOnInsert: new Date};
    } else {
      this.unset();
    }
  }
}