I am trying to write a toy program for submitting form and inserting into mongodb, but I keep getting DB error. Let me paste the relevant code here, and I hope to get some help.
I am using Sails ver 0.10.5 and the latest mongo 2.6.5, and my I run node on my Mac OSX 10.9:
Model: Employee.js
module.exports = {
attributes: {
name: {
type: "string",
required: true
},
email: {
type: "string",
required: true
},
password: {
type: "string",
required: true
}
},
beforeCreate: function(values, next) {
next();
}
};
route.js:
module.exports.routes = {
'/registeremployee': {
controller: 'employee',
action: 'register'
},
'/listemployees': {
controller: 'employee',
action: 'list_all'
}
};
EmployeeController.js
module.exports = {
index: function(req, res) {
res.send(200, {title: "employee index page"});
},
list_all: function(req, res) {
Employee.find().exec(function(err, employee) {
if (err) {
res.send(500, {title: 'error retrieving users'});
} else {
res.send(200, {'employees': employee});
}
});
},
register: function(req, res) {
if (req.method == "GET") {
res.view({title: "Form for registering employees"});
} else if (req.method == "POST") {
var username = req.param("username");
var password = req.param("password");
var email = req.param("email");
console.log("saving the username: " + username); //username printed as 'undefined'
Employee.create({username: username, password: password, email: email}).exec(function(error, employee) {
if (error) {
console.log('error');
res.send(500, {error: "DB error!"});
} else {
console.log('error');
res.send(200, employee);
console.log("saved employee: " + employee.username);
}
});
}
}
};
Lastly, the register.ejs template file:
<a href="/listemployees">List All Users</a>
<h2>Form - Create a User</h2>
<form action="/registeremployee" method="POST">
<table>
<tr><td>Name</td><td><input type=”text” name=”username” required></td></tr>
<tr><td>Password</td><td><input type=”password” name=”password” required></td></tr>
<tr><td>Email</td><td><input type=”email” name=”email” required></td></tr>
<tr><td></td><td><input type="submit"></td>
</table>
</form>
It looks to me that the form does not submit data, as the parameters are printed as undefined/null in my controller.
I have this in my connections.js:
mongo: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'sailsApp1'
}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
• `undefined` should be a string (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
var model = req.params.all();
for (var prop in model) {
console.log("property value is: " + model[prop]);
}
Employee.create(req.params.all()).exec(function(error, employee) {
if (error) {
return console.log(error);
} else {
res.send(200, employee);
console.log("saved employee: " + employee.name);
}
});
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
• `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
name: 'STRING',
email: 'STRING',
password: 'STRING'
name: {
type: 'STRING',
},
email: {
type: 'STRING',
email: true,
},
password: {
type: 'STRING',
},