Doubt in an error Node.js

Asked

Viewed 104 times

0

I’m starting to do projects with Node.js, but I don’t understand why my code isn’t generating the view, and I don’t think it’s 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>
  • Post the rest of the code with your controller, your view and your connection for a better evaluation.

  • Posted the view, the model, the Routes...

  • As you are using EJS check your configuration file is directed to the correct views folder, because by default it is directed to the root folder with the views name

  • Willian: app.set('view engine', 'ejs'); app.set('views', './app/views');

  • What the error ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.