简体   繁体   English

在保存到 Mongoose (ExpressJS) 之前如何格式化模型中的数据

[英]How to format data in Model before saving in Mongoose (ExpressJS)

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.我从用户那里以字符串格式获取日期,并且在创建 Schema 对象并保存之前,我目前在控制器中转换为日期。 Is there a way to move this logic to model as it seems to me that Model is the right place for this有没有办法将这个逻辑移动到模型中,因为在我看来 Model 是适合这个的地方

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

});

Currently I do this目前我这样做

//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);

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.虽然我不确定req.body.starttime的含义,但我很确定您正在寻找 Schema objects pre()函数,它是Mongoose Middleware 的一部分,并允许执行回调函数的定义在保存数据之前。 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();
});

Note that the callback function for the save event is called every time before a record is created or updated.请注意,每次创建或更新记录之前都会调用save事件的回调函数。 So this is for example the way for explicitly setting a "modified" timestamp.所以这是例如显式设置“修改”时间戳的方式。

EDIT:编辑:

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:如果您想在数据被分配并持久化到记录之前修改数据,您可以轻松利用 Schema 的set属性:

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

Assuming that the object util is within scope (required or whatever) your current implementation fits the signature for the property set :假设对象util在范围内(必需或其他),您当前的实现符合属性set的签名:

function set(val, 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.如果转换过程以任何方式依赖它,可选参数schemaType允许您检查架构字段定义的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM