Trying to model a relationship between collections by embedding documents but when validating in the schema and setting "required" to True, here comes the err
once I comment the required in genre object in movies schema the problem is solved but I want the validation
const Movie = mongoose.model(
'Movies',
new mongoose.Schema({
title: {
type: String,
required: true,
trim: true,
minlength: 1,
maxlength: 255
},
numberInStock: {
type: Number,
required: true,
min: 0,
max: 255
},
dailyRentalRate: {
type: Number,
required: true,
min: 0,
max: 255
},
genre: genreSchema
required: true
})
);
const genreSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 5,
maxlength: 50
}
});
TypeError: Invalid schema configuration: True is not a valid type at path required
you can use references and use populate when fetching
genre: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'genreSchema',
required: true
}],
Refer: Model Referenced one to Many Relationship between documents for better schema design
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With