it cant be that difficult, but I'm always getting the default mongoose 11000 error.
Here is a simplified version of my code:
model
import mongoose from 'mongoose';
import uniqueValidator from 'mongoose-unique-validator';
const UserSchema = new mongoose.Schema({
email: {
type: String,
index: true,
trim: true,
unique: true,
uniqueCaseInsensitive: true,
required: true
}
});
UserSchema.plugin(uniqueValidator);
controller
var data = {email: 'info@foobar.com'};
var user = new User(data);
user.save(function (err) {
console.log(err);
});
but then I'm always getting this
{
"code": 11000,
"index": 0,
"errmsg": "E11000 duplicate key error collection: portfolio:27017.users index: email_1 dup key: { : \"info@foobar.com\" }",
"op": {
"email": "info@foobar.com",
"_id": "58de95892be2a000d27ee3bc",
"__v": 0
}
}
instead of something like this
{
message: 'Validation failed',
name: 'ValidationError',
errors: {
username: {
message: 'Error, expected `username` to be unique. Value: `JohnSmith`',
name: 'ValidatorError',
kind: 'mongoose-unique-validator',
path: 'username',
value: 'JohnSmith'
}
}
}