I've defined a unique field in my model but when I tried to test it seems like it's not being checked by sails because I get a Error (E_UNKNOWN) :: Encountered an unexpected error:
MongoError: E11000 duplicate key error index:
instead a sails ValidationError.
What is the best way to handle a unique field in sails?
// model/User.js
module.exports{
attributes: {
email: {required: true, unique: true, type: 'email' },
....
}
// in my controller
User.create({email: 'hello@gmail.com'}).then(...).fail(....)
User.create({email: 'hello@gmail.com'}).then(...).fail(// throws the mongo error )
// and same goes with update it throws error
Thanks in advance guys.
The correct way to do this!!!
};
By this way we are not breaking concept of validator!
@tvollstaedt Your reponse worked like a charm, and by the way is the most elegant way to handle "uniqueness" in sailsjs so far.
Thank you!
Here is my two cents to add custom validation messages using "sails-validation-messages":
Then in the controller you can handle the error like this:
Thanks
The solutions by @tvollstaedt and David posted work but there is a big issue with this code. I was struggling with it all day so I am putting up this slightly modified answer. I would just comment but I do not have the points yet. I will gladly remove this answer if they can update theirs but I would really like to help someone who was having the same issues I've been having.
The problem with the above code, using the custom validator, is you cannot access the attribute "uniqueEmail" from the properties the way in witch both the previous solutions are trying to do. The only reason it is working in those solutions is because they are inadvertently throwing "uniqueEmail" into the global space.
The following is a slight modification of the tvollstaedt code that does not use the global space. It defines uniqueEmail outside the modual.exports therefore being scoped only to the module but accessible throughout the module.
There may yet be a better solution but this is the best I could come up with with minimal changing of an otherwise elegant solution.
The
unique
attribute currently only creates a unique index in MongoDB.You may use the
beforeValidate()
callback to check for an existing record with that attribute and save the result in a class variable.This approach makes sure that your model returns a proper validation error which can be evaluated by clients.
EDIT
As thinktt pointed out, there was an error in my former solution which rendered the default
uniqueEmail
value useless since it is defined within the model declaration itself and therefore cannot be referenced within the model code. I've edited my answer accordingly, thanks.You are trying to create two users with the same email address after defining the email as a unique field.
Maybe you can query for a user by that email address - if it exists already - return an error or update that user.
Sails.js & MongoDB: duplicate key error index
There is also an interesting bit about unique model properties in the Sails.js docs https://github.com/balderdashy/waterline#indexing
EDIT: Pulled from http://sailsjs.org/#!documentation/models
Available validations are:
empty, required, notEmpty, undefined, string, alpha, numeric, alphanumeric, email, url, urlish, ip, ipv4, ipv6, creditcard, uuid, uuidv3, uuidv4, int, integer, number, finite, decimal, float, falsey, truthy, null, notNull, boolean, array, date, hexadecimal, hexColor, lowercase, uppercase, after, before, is, regex, not, notRegex, equals, contains, notContains, len, in, notIn, max, min, minLength, maxLength