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