Aquí está mi controlador. Puedo obtener datos que ya están en la base de datos, pero el problema es que los datos no pueden aparecer en mi vista.
const Comment = require('../models/comments.js'); exports.getComments = (req, res, next) =>{ Comment.fetchAll() .then(rows => { res.render('display', { data: rows, }); console.log(rows) }) .catch(err => console.log(err)); };Y aquí está mi vista
<% if(data.length > 0) { %> <table class="table"> <thead> <tr> <th scope="col">#</th> <th scope="col">Name</th> <th scope="col">Email</th> <th scope="col">Comment</th> </tr> </thead> <tbody> <% for(var i = 0; i< data.length; i++) { %> <tr> <th scope="row"><%= (i+1) %></th> <td><%= data[i].name%></td> <td><%= data[i].email%></td> <td><%= data[i].comment%></td> </tr> <% } %> </tbody> </table> <% } %> <!-- if result is empty --> <% if(!data.length) { %> <p class="text-center">No comments found!</p> <% } %> No sé por qué obtengo campos vacíos en la vista porque cuando console.log(rows) obtengo datos.
El problema era que mi vista esperaba una matriz [rows] . Así que tuve que pasar una matriz como devolución de llamada.
exports.getComments = (req, res, next) =>{ Comment.fetchAll() .then(([rows]) => { res.render('display', { data: rows, }); console.log(rows) }) .catch(err => console.log(err)); };