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?
The field "code" is validated even if it is undefined so you must check if it has a value:
LocationSchema.path('code').validate(function(code) {
return code && code.length === 2;
}, 'Location code must be 2 characters');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句