Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 957
ygrunin Avatar asked Apr 16 '15 13:04

ygrunin


3 Answers

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
     }
}); 
like image 129
jaumard Avatar answered Oct 19 '22 09:10

jaumard


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.

like image 21
ygrunin Avatar answered Oct 19 '22 11:10

ygrunin


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 },

like image 1
simondelliott Avatar answered Oct 19 '22 10:10

simondelliott