首页 文章

“email”验证规则崩溃sails服务器 - Mongo与Sails.js

提问于
浏览
6

当电子邮件验证规则在sails.js的模块上失败时,服务器崩溃了 . 这是我模块的片段://用户的电子邮件地址

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

错误如下:

错误:错误(E_VALIDATION):: 1属性在WLValidationError.WLError无效(C:\ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ error \ WLError.js:26 :15)在C:\ Users \ yuri \的新WLValidationError(C:\ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ error \ WLValidationError.js:20:28) 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)完成后(C:\ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js:135:19)at 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 \ a sync \ lib \ async.js:135:19)在C:\ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js:32:16 at C:\ Users \ yuri位于C:\ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js:125:13位于_each的Array.forEach(native)(C:\ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js:46:24)在验证时的Object.async.each(C:\ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js:124:9)处(C:\ Users \ yuri \ AppData \漫游\ npm \ node_modules \ sails \ node_modules \ waterline \ lib \ waterline \ core \ validations.js:156:11)在C:\ Users \ yuri \ AppData \ Roaming \ npm \ node_modules \ sails \ node_modules \ async \ lib \ async.js:125:13发送给用户的无效属性:•email•undefined应该是一封电子邮件(而不是“admin @ gmailasd”,这是一个字符串)

3 回答

  • 2

    声明电子邮件字段的正确方法如下:

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

    你可以在这里看到所有不同的属性类型http://sailsjs.org/documentation/concepts/models-and-orm/attributes

    如果要捕获插入/更新错误,可以在控制器上执行此操作:

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

    她的答案 . 感谢jaumard,我发现了这个问题 . 我在错误中使用了未定义的字段,而没有在err.originalError.code之前检查是否存在但是它未定义 . 所以正确的方法是:err.originalError && err.originalError.code && err.originalError.code === 11000

    而不是err.originalError.code === 11000 .

  • 7

    以前版本的Sails建议电子邮件验证是这样实现的

    电子邮件:{type:'string',email:true,required:true},

    当前版本应该是这样的

    电子邮件:{type:'email',required:true},

相关问题