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

妖精的绣舞 提交于 2019-12-23 08:07:21

问题


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)


回答1:


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
     }
}); 



回答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.




回答3:


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



来源:https://stackoverflow.com/questions/29675698/email-validation-rule-crash-sails-server-mongo-with-sails-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!