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> &nbsp; &nbsp;at model.document.invalidate (c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1162:32)     <br> &nbsp; &nbsp;at c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1037:16         <br> &nbsp; &nbsp;at validate (c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:651:7)             <br> &nbsp; &nbsp;at c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:679:9                 <br> &nbsp; &nbsp;at array.foreach (native)                     <br> &nbsp; &nbsp;at schemastring.schematype.dovalidate (c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\schematype.js:656:19)                         <br> &nbsp; &nbsp;at c:\users\lotus\desktop\mastering_mean\application\node_modules\mongoose\lib\document.js:1035:9                             <br> &nbsp; &nbsp;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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -