我有一个具有开始时间和结束时间的事件模型。该事件必须在同一天开始和结束,因此我在模型中创建了一个.pre('validate')
函数,如下所示:
let eventSchema = new Schema(
{
description: { type: String, required: true },
start_time: { type: Date, required: true },
end_time: { type: Date, required: true }
}
);
eventSchema.pre('validate', function(next) {
if (!this.start_time.getDate() === this.end_time.getDate()) {
next(new Error('Event must end on the same day.'))
} else {
next()
}
})
但是,我能够保存一个带有两个不同日期的事件。我将验证器放在错误的位置吗?