I'm starting to do projects with node.js, but I do not understand why my code is not generating the view, and I'm not even storing the data in sql.
function Funcionarios (connection) {
this._connection = connection;
}
Funcionarios.prototype.getFuncionarios = function(callback) {
this._connection.query('SELECT * FROM crud', callback);
}
Funcionarios.prototype.salvarFuncionario = function(funcionario, callback) {
this._connection.query('INSERT INTO crud SET ?',funcionario, callback);
}
module.exports = function() {
return Funcionarios;
}
module.exports = function(application){
application.get('/crud', function(req,res){
res.render('/crud');
});
application.post('/funcionarios/salvar', function(req,res){
var funcionario = req.body;
var connection = application.config.dbConnection();
var employeeModel = new
application.app.models.employeeModel(connection);
employeeModel.salvarFuncionario(funcionario, function(error, result){
res.redirect('/funcionarios');
});
});
}
module.exports = function(application){
application.get('/funcionarios', function(req,res){
var connection = application.config.dbConnection();
var employeeModel = new
application.app.models.employeeModel(connection);
employeeModel.getFuncionarios(function(error, result){
res.render('/funcionarios', { funcionarios : result });
console.log(funcionarios);
});
});
}
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8"/>
<title>Funcionarios</title>
</head>
<body>
<h1>Funcionarios</h1>
<table>
<tr>
<td>ID</td>
<td>Título</td>
</tr>
<% for(var i = 0; i < funcionarios.length; i++){ %>
<tr>
<td><%= funcionario[i].employeeID %></td>
<td><%= funcionario[i].employeeName %></td>
<td><%= funcionario[i].employeePosition %></td>
<td><%= funcionario[i].employeePermission %></td>
</tr>
<% } %>
</table>
</body>
</html>