I'm making a Blog with Node.js and a MongoDB (Mongoose) database in which users can share their blog.
I HAD a SubmittedBlog model like this:
const SubmittedBlogSchema = new mongoose.Schema({
blogType: {
type: String,
required: true
},
text: {
type: String,
required: [true, 'Please enter a text'],
minlength: [50, 'Text must be at least 50 characters long'],
maxlength: [2000, 'Text must be at most 2000 characters long'],
trim: true
},
...
});
But then I had to add another field object called submitter which contains information about the blog submitter:
const SubmittedBlogSchema = new mongoose.Schema({
blogType: {
type: String,
required: true
},
submitter: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true
},
username: {
type: String,
required: true
}
},
...
});
Note that this object is required. However If I want to SubmittedBlog.findById() now, it isn't working and it errors out that "Path submitter.username is required". I thought that this would only happen at saving a submitted blog but why is this at finding a blog???