I tested the following Employee.js model in Sails.js and I found something that seems hard for me to understand.
When the form for creating a new Employee is posted, I got the following errors with the 1st definition of Employee.js model, but the 2nd definition of Employee.js works. So what's the difference between the two definitions of Employee.js?
Errors:
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
Definition 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
}
}
}
};
Definition2: Employee.js
module.exports = {
attributes: {
name: 'STRING',
email: 'STRING',
password: 'STRING',
toJSON: function() {
var obj = this.toObject();
return {
name: obj.name,
email: obj.email,
password: obj.password
}
}
}
};
The form for creating a new Employee is as following:
<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>