Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Can't get fields on display.ejs. I only get empty rows

Here is my controller. I can get data that is already in the database but problem is that data cant appear in my view.

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));
};

And here is my view

 <% 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> 
        <% } %>

I don't know why I'm getting empty fields in the view because when i console.log(rows) I get data.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Problem was my view was expecting an array [rows] . So I had to pass an array as callback.

exports.getComments = (req, res, next) =>{
    Comment.fetchAll()
        .then(([rows]) => {
            res.render('display', {
                data: rows,
            });
            console.log(rows)
        })
        .catch(err => console.log(err));
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda