"email" validation rule crash sails server - Mongo with Sails.js
Asked Answered
A

3

7

while the email validation rule fails on module of the sails.js, the server is crashing. Here the snippet of my module: // The user's email address

email: {
  type: 'string',
  email: true,
  required: true,
  unique: true
},

And the error as below :

err: Error (E_VALIDATION) :: 1 attribute is invalid at WLValidationError.WLError (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\error\WLError.js:26:15) at new WLValidationError (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\error\WLValidationError.js:20:28) at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\query\validate.js:45:43 at allValidationsChecked (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:203:5) at done (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:135:19) at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:32:16 at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:184:23 at done (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:135:19) at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:32:16 at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:157:64 at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:125:13 at Array.forEach (native) at _each (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:46:24) at Object.async.each (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:124:9) at validate (C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:156:11) at C:\Users\yuri\AppData\Roaming\npm\node_modules\sails\node_modules\async\lib\async.js:125:13 Invalid attributes sent to User: • email • undefined should be a email (instead of "admin@gmailasd", which is a string)

Aviation answered 16/4, 2015 at 13:8 Comment(0)
T
7

The correct way to declare an email field is like this :

email: {
  type: 'email',
  required: true,//Email field will be required for insert or update  
  unique: true //Insert or update will crash if you try to insert duplicate email
},

You can see all different attribut types here http://sailsjs.org/documentation/concepts/models-and-orm/attributes

If you want to catch insert/update errors you can do this on your controller :

MyModel.create({email:email}).exec(function(err, model)
{
     if(err)
     {
       //Check if it's a validation error or a crash 
       if(err.code == "E_VALIDATION")
         sails.log.debug("valid fail, check form");
       else 
         sails.log.debug("crash");
     }
     else
     {
       //do what you want to do with the data
     }
}); 
Teodora answered 16/4, 2015 at 13:16 Comment(7)
It is crushing still:(Aviation
Normal if you don't specify on email who is required validation throw an exception it's normalTeodora
If you don't want if just try catch your create/update methodTeodora
Can i add the exception handler on model? Or i should do this in Controller?Aviation
I think you can only do it on controller not on modelTeodora
Add some explanation to your answer please.Verbenaceous
email: true - it is no longer necessary. Only type: 'email'Spoilsman
A
2

Her the answer. Thanks to jaumard, i found the problem. I used undefined field in error, without checking if exists before err.originalError.code but it was undefined. So the correct way is : err.originalError && err.originalError.code && err.originalError.code === 11000

and not err.originalError.code === 11000.

Aviation answered 16/4, 2015 at 13:58 Comment(0)
R
1

Previous versions of Sails recommended that email validation was achieved like this

email: { type: 'string', email: true, required: true },

The current version should be like this

email: { type: 'email', required: true },

Roundabout answered 4/9, 2015 at 14:43 Comment(1)
Filed an issue for documentation on the email attribute.Waechter

© 2022 - 2024 — McMap. All rights reserved.