I have created_at
date, duration
by minutes set by admin and expires_at
properties, I have written a logic to calculate the expires_at
property and its working fine, also I need to set expired
as a boolean to set to true
if now date is greater than expires_at
I have written a function and its working but I don't know if mongoose can trigger it or when?
so the question is how can I update expired
property to true
when current time is passed the expires_at
time ?
const PollSchema = new mongoose.Schema({
question: {
type: String,
required: true,
},
options: {
type: Array,
required: true,
},
expires_at: {
type: Date,
},
expired: {
type: Boolean,
default: false // how I can update this value to true when now > expires_at;
},
duration: {
type: Number,
required: true,
},
});
PollSchema.pre('save', function (next) {
const poll = this;
poll.expired = expired(poll);
next();
});
PollSchema.pre('save', function (next) {
const poll = this;
var now = new Date();
poll.expires_at = now.setMinutes(now.getMinutes() + poll.duration);
next();
});
// I am triggering this function on save, but I think it should be triggered when now > poll.expires_at;
PollSchema.pre('save', function (next) {
const poll = this;
const now = new Date();
poll.expired = now > poll.expires_at;
next();
});