mongoose getting Model validation failed instead of custom error message -
i'm sure issue password validator because if comment out document gets inserted. if enter valid password works. here entire userschema
var userschema = new schema({ firstname: string, lastname: string, email: { type: string, unique: true, match: /.+\@.+\..+/ }, website: { type: string, set: urlmodifier }, username: { type: string, trim: true, required: true, unique: true }, password: { type: string, validate: [ function(password) { return password.length >= 6; }, 'password should longer' ] }, createdat: { type: date, default: date.now }, role: { type: string, enum: ['admin', 'owner', 'user'] } });
this issue i'm having instead of custom error message get
validationerror: user validation failed <br> at model.document.invalidate (c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1162:32) <br> at c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1037:16 <br> at validate (c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:651:7) <br> at c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:679:9 <br> at array.foreach (native) <br> at schemastring.schematype.dovalidate (c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:656:19) <br> at c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1035:9 <br> at process._tickcallback (node.js:355:11)
mongoose version ^4.05
not sure if bug, changed in api, or i'm doing wrong.
no mention of in particular tutorial i'm learning from.
http://mongoosejs.com/docs/validation.html
the custom error message available in validation error object returned save(err) function in controller err.
basic
user.save(function(err) { if(err) { return next(err); } else { res.json(user); } });
will return default error message "user validation failed"
this
user.save(function(err) { if(err) { console.log(err.errors.password.message); return next(err); } else { res.json(user); } });
will return custom error message
Comments
Post a Comment