Omid
Omid

Reputation: 321

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

Upvotes: 6

Views: 37687

Answers (9)

Ravi Kumar
Ravi Kumar

Reputation: 39

Your forget to put "," in end of genre:genreSchema or rewrite your code like

genre: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'genreSchema',
        required: true
    }],

Upvotes: -1

user21290379
user21290379

Reputation: 1

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}]

*/

Upvotes: 0

Rajiv Ranjan
Rajiv Ranjan

Reputation: 1

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.

Upvotes: 0

user16352740
user16352740

Reputation:

because you forgot to put "," here:

genre: genreSchema <<<here
required: true

should be:

genre: genreSchema,
required: true

Upvotes: 1

K.Nehe
K.Nehe

Reputation: 454

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
});

Upvotes: 3

Anshik gupta
Anshik gupta

Reputation: 131

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.

Upvotes: 0

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

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

Upvotes: 5

Hassan Ali
Hassan Ali

Reputation: 1029

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.

Upvotes: 1

Venky
Venky

Reputation: 59

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

Upvotes: 2

Related Questions