So, according to mongoose docs , you should set up a custom error message in the schema as follows:
var breakfastSchema = new Schema({ eggs: { type: Number, min: [6, 'Too few eggs'], max: 12 }, bacon: { type: Number, required: [true, 'Why no bacon?'] } });
So, I wanted to do something like this:
var emailVerificationTokenSchema = mongoose.Schema({ email: {type: String, required: true, unique: [true, "email must be unique"]}, token: {type: String, required: true}, createdAt: {type: Date, required: true, default: Date.now, expires: '4h'} });
The idea is that when you try to save one of these tokens, and there is already a conflict, he deflates an error message stating that "the letter must be unique."
However, when I do something like this (where I save the token with the same email address):
verificationToken.save( function (err) { if (err) { return console.log(err); } else { return console.log(err); } });
I keep getting the following:
'E11000 duplicate key error: index ___.emailVerificationToken.$email_1 dup key: { : " _____@wdad.com
Any thoughts? Unique parameter not supported for custom messages? Is this a viable way to go about things?