LearnBoost/mongoose — Issue #705: Converting Epoch values to dates and viceversa
Author: johnhamelink
Created: 2012-02-01 11:24:09+00:00
State: open

I have a schema which contains (amongst other things) `timeApproved: { type: Date }`. I want to convert epochs to the Date format as a middleware so that any insert and update will automatically do the conversion.

I tried doing the following:

researchSchema.plugin(this.obj.useTimestamps);
researchSchema.pre('save',function(next){
this.timeApproved = new Date(this.timeApproved*1000).toISOString();
next();
});

flow.model = this.obj.mongoose.model(this.moduleName,researchSchema);

However, this seems to have zero effect on what gets inserted into mongoDB.

Another thing, is that when I submit this integer that doesn't get converted, it passes validation! How is that possible?