Reputation: 321
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
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
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
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
Reputation:
because you forgot to put "," here:
genre: genreSchema <<<here
required: true
should be:
genre: genreSchema,
required: true
Upvotes: 1
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
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
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
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
Reputation: 59
Try giving the message next to true. For example--> required: [true, "Title required"]
Upvotes: 2