I am trying embed a document, so I guess a subdocument, into a Mongoose schema. I have been following this pattern that the Mongoose documentation suggests. My schemas look like this.
var CategoriesSchema = new Schema({
name: { type: String, required: true, default: 'default' }
});
var PostSchmea = new Schema({
title: { type: String, required: true, default: 'default title' },
writtenBy: { type: ObjectId, required: true, default: '55878003ebf4b06110ef2ff8' },
publishedOn: { type: Date, default: Date.now },
updatedOn: Date,
published: { type: Boolean, required: true, default: false },
content: { type: String, required: true, default: 'empty' },
category: { type: [CategoriesSchema] },
tags: [String],
images: [String]
});
As you can see I have the CategoriesSchema embedded into the PostSchema. My question is how do I set a default for the category key inside of the PostSchema. I tried the following and got the error "TypeError: Cannot read property '$__' of undefined":
var PostSchmea = new Schema({
title: { type: String, required: true, default: 'default title' },
writtenBy: { type: ObjectId, required: true, default: '55878003ebf4b06110ef2ff8' },
publishedOn: { type: Date, default: Date.now },
updatedOn: Date,
published: { type: Boolean, required: true, default: false },
content: { type: String, required: true, default: 'empty' },
category: { type: [CategoriesSchema], default: [CategoriesSchema] },
tags: [String],
images: [String]
});
And I also tried the following and got the error "SyntaxError: Unexpected token":
var PostSchmea = new Schema({
title: { type: String, required: true, default: 'default title' },
writtenBy: { type: ObjectId, required: true, default: '55878003ebf4b06110ef2ff8' },
publishedOn: { type: Date, default: Date.now },
updatedOn: Date,
published: { type: Boolean, required: true, default: false },
content: { type: String, required: true, default: 'empty' },
category: { type: [CategoriesSchema], default: new CategoriesSchema(name: "default") },
tags: [String],
images: [String]
});
Any suggestions or documentation would be greatly appreciated.
Thanks,