model - Node.js - Mongoose path validation failing - TypeError: Cannot call method 'validate' of undefined -
i have next mongoose schema , path validaion:
var locationschema = new schema({ userid: { type: number, required: true }, location: { type: [{ type: "string", required: true, enum: ['point', 'linestring', 'polygon'], default: 'point' }], coordinates: { type: [number], required:true } }, tags: [{ type: string, index: true, required: true }], create_date: { type: date, default: date.now } }); locationschema.path('location.coordinates').validate(function(coordinates){ homecoming coordinates && coordinates.tostring().match(/([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/g); }, 'invalid latitude or longitude.');
when start app get:
locationschema.path('location.coordinates').validate(function(coordinates){ ^ typeerror: cannot phone call method 'validate' of undefined
can advise why failing? note validate path('location'), starts fine.
to define field in embedded object named type
, need define type using explicit object notation or mongoose thinks it's defining type of parent object instead:
var locationschema = new schema({ userid: { type: number, required: true }, location: { type: { type: [{ type: "string", required: true, enum: ['point', 'linestring', 'polygon'], default: 'point' }]}, coordinates: { type: [number], required:true } }, tags: [{ type: string, index: true, required: true }], create_date: { type: date, default: date.now } });
node.js model mongoose typeerror
No comments:
Post a Comment