'Unable to change createdAt date to unix timestamp with mongoose
The mongoose documentation has a section about changing the timestamps property who is setting a Date for createdAt and updatedAt to a UNIX timestmap.
https://www.mongoosejs.cn/docs/guide.html#timestamps
const schema = Schema({
createdAt: Number,
updatedAt: Number,
name: String
}, {
// Make Mongoose use Unix time (seconds since Jan 1, 1970)
timestamps: { currentTime: () => Math.floor(Date.now() / 1000) }
});
My code
const userAccountSchema = new mongoose.Schema({
email: {
type: String,
required: [true, 'Please add an email'],
unique: true,
match: [/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'Please add a valid email']
},
password: {
type: String,
minlength: 6,
select: false
},
is_active: {
type: Boolean,
default: false
},
onboarding_completed: {
type: Boolean,
default: false
},
confirm_token: String,
reset_password_token: String,
reset_password_expire: Date,
}, {
timestamps: { currentTime: () => Math.floor(Date.now() / 1000) }
});
The result is a record with a Date for createdAt and updatedAt instead of a timestamp.
"createdAt": "1970-01-20T03:09:20.506Z"
"updatedAt": "1970-01-20T03:09:20.506Z"
Can someone help me out?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
