4

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

Omid
  • 301
  • 1
  • 2
  • 12
  • 1
    `required: true` looks like a field but that is the reason you are getting invalid schema configuration error. – Saroj May 29 '19 at 09:04

8 Answers8

5

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

Naing Lin Aung
  • 3,373
  • 4
  • 31
  • 48
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
2

Try giving the message next to true. For example--> required: [true, "Title required"]

Venky
  • 51
  • 2
  • 8
2

Remove required: true and Follow this from the official docs. Note the version of mongoose used. I got the same error and solved it. My mistake was making the subdocument a model instead of keeping it as a schema

const childSchema = new Schema({ name: 'string' });

const parentSchema = new Schema({
  // Array of subdocuments
  children: [childSchema],
  // Single nested subdocuments. Caveat: single nested subdocs only work
  // in mongoose >= 4.2.0
  child: childSchema
});
K.Nehe
  • 424
  • 10
  • 22
1

Please share full code.

Maybe the main reason is that u not used

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: {
        ref: 'SchemaName',
        required: true
    },
  })
);

You can do this like this.

Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
Hassan Ali
  • 994
  • 6
  • 22
0

Export the genre model as exports.genreSchema = genreSchema

Then in movies.js file import the model as import { genreSchema } from './genre.model'

Try doing this the error will not come.

alexherm
  • 1,362
  • 2
  • 18
  • 31
0

because you forgot to put "," here:

genre: genreSchema <<<here
required: true

should be:

genre: genreSchema,
required: true
0

If you are facing "required:true" problem

Don't pass the function new mongoose.Schema({}). Instead of this you just have to make const Schema = mongoose.Schema; then use Schema({}).

It will definitely work.

Laurel
  • 5,965
  • 14
  • 31
  • 57
0

A simple way to solve it.

import { Schema, model, Model } from 'mongoose';

    const obj1Scheema:Schema = new Schema({
        genre:{type:genreSchema.schema, ... rest of the presets} 
        GenreArr: {type:[genreSchema.schema], ... rest of the presets}
    })

    //genre schema:
    const genreSchema = new mongoose.Schema({
         name: {
         type: String,
         required: true,
         minlength: 5,
         maxlength: 50
     }
   });

/*
        genre:{ ... genre fields} 
        GenreArr:[{... genre fields}]

*/
Eric Aya
  • 69,473
  • 35
  • 181
  • 253