-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique index validation error not throwing #1225
Comments
Setting |
how can i catch this error? |
doc.save(function (err) {
// handle err
}) |
i tryed it and the error didin't bubbled. /**
* Unique Validation
* @param {Schema} schema The schema to witch validate.
* @param {Model} model The model to perform searches.
* @param {String} field The field name to search for duplicated entries.
* @throws {E11000} Duplicate key.
*/
function uniqueValidator(schema, model, field) {
schema.pre('save', function(next){
var toValidate = {};
toValidate[field] = this[field];
model.findOne(toValidate, field, function(err, res){
if (err) {
next(err);
} else if (res) {
err = new Error('Duplicate key.');
err.code = 'E11000';
next(err);
} else {
next();
}
});
});
} it's not best practices but is working. i just wanted to know if there was some "cleaner"/best way to do this. thanks. |
check the indexes that exist in your collection to see if the unique index exists.
You should see all indexes that exist on your collection. Check to see if your unique index exists. If it does, something is very wrong. If it doesn't (more likely), there are probably duplicate values in the collection already which will prevent a unique index from being created. You can check this by adding a listener to your models var schema = Schema(..);
var YourModel = mongoose.model('YourModel', schema);
YourModel.on('index', function (err) {
if (err) console.error(err);
}) http://mongoosejs.com/docs/api.html#model_Model.ensureIndexes |
thanks. that's what heppened, first i added non-unique values, and then changed the code adding |
I don't understand your question. |
as there is a duplicate key and it have to be unique, how do i stop the saving operation so it isin't saved, or what alternative should i try? |
I think it would be great if the unique error was thrown the same way of other validators. Mongoose could check if the error code is 11001 (http://www.mongodb.org/about/contributors/error-codes/) and then call something like: doc.invalidate(path, "unique"). I tried doing this with schema.post('save', ...) but it seems that no post handlers are called on error. |
sorry, the error was mine. |
For anyone interested, I’ve added a plugin which checks the DB prior to saving and throws a Mongoose validation error if any of your unique constraints will be violated: |
I had this issue and it was due to already existing duplicate values in my index (as pointed out in aheckmann's comment on Feb 1, 2013). I was reading from here http://mongoosejs.com/docs/api.html#schematype_SchemaType-required where it mentions the error, but fails to mention about already exiting duplicates preventing the uniques index from being created in the first place. Might be worth updating the doc to mention this too! |
Can we make a custom message option for this error just like we can handle the "require" attribute? |
@ahmedkrdzalic we opened a new issue to track that feature request. In the meantime, please check out this plugin: https://www.npmjs.com/package/mongoose-beautiful-unique-validation |
I'm using this schema with latest npm mongoose:
If I try to save a email that is already in db, I expect to get a
ValidationError
like if arequired
field is omitted. However this is not the case, I get aMongoError: E11000 duplicate key error index
.Which is not a validation error (happens even if I remove the unique:true).
The text was updated successfully, but these errors were encountered: