My validation is:
LocationSchema.path('code').validate(function(code) {
return code.length === 2;
}, 'Location code must be 2 characters');
as I want to enforce that the code
is always 2 characters.
In my schema, I have:
var LocationSchema = new Schema({
code: {
type: String,
trim: true,
uppercase: true,
required: true,
},
I'm getting an error: Uncaught TypeError: Cannot read property 'length' of undefined
however when my code runs. Any thoughts?
Much simpler with this:
var LocationSchema = new Schema({
code: {
type: String,
trim: true,
uppercase: true,
required: true,
maxlength: 2
},
https://mongoosejs.com/docs/schematypes.html#string-validators
The exact string length is like:
...
minlength: 2,
maxlength: 2,
...
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