如何在保存Mongoose(ExpressJS)之前格式化模型中的数据 [英] How to format data in Model before saving in Mongoose (ExpressJS)

查看:156
本文介绍了如何在保存Mongoose(ExpressJS)之前格式化模型中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以字符串格式从用户获取日期,并且我在创建Schema对象并保存之前当前转换为控制器中的Date。有没有办法将这个逻辑转移到模型,因为在我看来,Model是适合这个的地方

I get Date from user in the string format and I currently convert into a Date in controller before creating the Schema object and saving. Is there a way to move this logic to model as it seems to me that Model is the right place for this

var RunSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    starttime: {
        type: Date,
        default: Date.now
    }

});

目前我这样做

//req.body = {starttime;'2.05.2013 11:23:22'}
var run = new Run(req.body);
// util.getDate(datetime) returns false if invalid and Date() if valid
// req.body.starttime = '2.05.2013 11:23:22';
run.starttime = util.getDate(req.body.starttime);
run.save(function(err) {
    if(err) {
    } else {
    }
});

在旁注中,如何在自定义函数检查中处理参数时如何断言。类似

On a sidenote, how do I assert if I want to process the param in custom function checks. Something like

    req.assert('name', 'Name can\'t be empty').len(1, 1000);


推荐答案

虽然我不确定<的含义code> req.body.starttime ,我很确定你正在寻找Schema对象 pre()的功能,这是 Mongoose Middleware 的一部分,允许在保存数据之前执行回调函数的定义。可能类似这样的工作:

While I'm not sure about the meaning of req.body.starttime, I'm pretty sure you're looking for the Schema objects pre() function which is part of the Mongoose Middleware and allows the definition of callback functions to be executed before data is saved. Probably something like this does the desired job:

var RunSchema = new Schema({
  [...]
  starttime: {
    type: Date,
    default: Date.now
  }
});

RunSchema.pre('save', function(next) {
  this.starttime = new Date();
  next();
});

请注意保存的回调函数每次创建或更新记录之前都会调用event。因此,这是明确设置修改时间戳的方式。

Note that the callback function for the save event is called every time before a record is created or updated. So this is for example the way for explicitly setting a "modified" timestamp.

编辑:

感谢你的评论,我现在对你想要达到的目标有了更好的理解。如果您想在分配数据并将其保留到记录之前修改数据,您可以轻松利用模式的 set 属性:

Thanks to your comment, I now got a better understanding of what you want to achieve. In case you want to modify data before it gets assigned and persisted to the record, you can easily utilize the set property of the Schema:

// defining set within the schema
var RunSchema = new Schema({
  [...]
  starttime: {
    type: Date,
    default: Date.now,
    set: util.getDate
  }
});

假设对象 util 在范围内(必需或其他)您当前的实现符合属性的签名 set

Assuming that the object util is within scope (required or whatever) your current implementation fits the signature for the property set:

function set(val, schemaType)

可选参数 schemaType 允许您检查架构字段定义的属性(如果转换过程以任何方式依赖于它)。

The optional parameter schemaType allows you to inspect the properties of your schema field definition if the transform process depends on it in any way.

这篇关于如何在保存Mongoose(ExpressJS)之前格式化模型中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆