javascript – Sails.js中的模型验证失败

前端之家收集整理的这篇文章主要介绍了javascript – Sails.js中的模型验证失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在Sails.js中测试了以下Employee.js模型,我发现了一些我觉得难以理解的东西.

当发布用于创建新Employee的表单时,我在Employee.js模型的第一个定义中遇到以下错误,但Employee.js的第二个定义有效.那么Employee.js的两个定义之间的区别是什么?

错误

Error (E_VALIDATION) :: 3 attributes are invalid
    at WLValidationError.WLError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:33:18)
    at new WLValidationError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLValidationError.js:20:28)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:45:43
    at allValidationsChecked (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:195:5)
    at done (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:135:19)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:32:16
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:186:14
    at done (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:135:19)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:32:16
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:157:64

Invalid attributes sent to Employee:
 • name
   • `undefined` should be a string (instead of "null",which is a object)
   • "required" validation rule Failed for input: null
 • email
   • `undefined` should be a email (instead of "null",which is a object)

   • "required" validation rule Failed for input: null
 • password
   • `undefined` should be a string (instead of "null",which is a object)
   • "required" validation rule Failed for input: null

定义1:Employee.js

module.exports = {
  attributes: {

    name: {
      type: 'STRING',},email: {
      type: 'STRING',email: true,password: {
      type: 'STRING',toJSON: function() {
      var obj = this.toObject();

      return {
        name: obj.name,email: obj.email,password: obj.password
      }
    }

  }
};

定义2:Employee.js

module.exports = {
  attributes: {

    name: 'STRING',email: 'STRING',password: 'STRING',password: obj.password
      }
    }

  }
};

创建新员工的表单如下:

<form action="/signupemployee" method="POST">
  <table>
    <tr><td>Name</td><td><input type=”text” name=”name”></td></tr>
    <tr><td>Password</td><td><input type=”password” name=”password”></td></tr>
    <tr><td>Email</td><td><input type=”email” name=”email”></td></tr>
    <tr><td></td><td><input type="submit"></td>
  </table>
</form>

解决方法

应该存在一个差异.在您的第二个模型中,email属性不需要包含有效电子邮件格式的字符串.据我所知,定义如:
name: {
    type: "STRING"
}

name: "STRING"

旨在相同,所以这可能是Waterline中的一个错误.

虽然这个问题不久前被问过,但仍然值得提出一个问题来确保它得到解决.

原文链接:https://www.f2er.com/js/157311.html

猜你在找的JavaScript相关文章