shema.pre('validate')不会停止保存不同日期的日期

时间:2018-06-24 16:26:31

标签: mongodb mongoose nosql

我有一个具有开始时间和结束时间的事件模型。该事件必须在同一天开始和结束,因此我在模型中创建了一个.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()
    }
})

但是,我能够保存一个带有两个不同日期的事件。我将验证器放在错误的位置吗?

0 个答案:

没有答案