mongoose-duplicate-key-error
v1.0.2
Published
Convert a MongoDB duplicate key error to a mongoose validation error
Downloads
10
Readme
mongoose-duplicate-key-error
Convert a MongoDB duplicate key error to a mongoose validation error.
npm install mongoose-duplicate-key-error
Usage
Only mongo errors with code 11000
or 11001
are transformed, any other error (or generic object) is returned as is.
var duplicateError = require('mongoose-duplicate-key-error');
User.create({ email: '[email protected]' }, function(err, model) {
err = duplicateError(err, {
model: User,
path: 'email',
message: 'E-mail already in use'
});
if(err) throw err;
console.log(model);
});
Pass additional options to the function to return errors with better messages.