Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose validation err: Invalid schema configuration

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

like image 682
Omid Avatar asked Jul 17 '26 12:07

Omid


1 Answers

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

like image 91
Thamaraiselvam Avatar answered Jul 20 '26 00:07

Thamaraiselvam