Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  27] [ 2]  / answers: 1 / hits: 52226  / 9 Years ago, mon, april 27, 2015, 12:00:00

In a mongoose schema such as:





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'
}]
});





On the line for the end field the default date should set to +7 days. I can add presave hook and set it there, but wondering if theres a way to do this inline in the default field.


More From » mongoose

 Answers
1

You can add 7 days converted to milliseconds to current date like this



default: new Date(+new Date() + 7*24*60*60*1000)


or even like this



default: +new Date() + 7*24*60*60*1000


UPDATE



Please check the @laggingreflex comment below. You need to set function as default value:



default: () => new Date(+new Date() + 7*24*60*60*1000)

[#66865] Sunday, April 26, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elishaannac

Total Points: 28
Total Questions: 97
Total Answers: 101

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
elishaannac questions
Sun, Dec 5, 21, 00:00, 3 Years ago
Mon, Jun 14, 21, 00:00, 3 Years ago
Mon, Jul 22, 19, 00:00, 5 Years ago
Mon, Jul 8, 19, 00:00, 5 Years ago
;