我在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>