在 mongoose schema 中,例如:
var EventSchema = new Schema({
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
description: {
type: String,
default: '',
trim: true
},
start: {
type: Date,
default: Date.now,
required: 'Must have start date - default value is the created date'
},
end: {
type: Date,
default: Date.now + 7 Days, // Date in one week from now
required: 'Must have end date - default value is the created date + 1 week'
},
tasks: [{
type: Schema.ObjectId,
ref: 'Task'
}]
});
在"结束"字段的行上,默认日期应设置为+7天。我可以添加预保存挂钩并在那里设置它,但想知道是否有一种方法可以在线设置默认字段。
() => Date.now()
重构了代码。 - AWE FRANCIS OLAWUMI