Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

javascript - 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. 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
    }

});

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
});

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.

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:

// 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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...