'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.



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saliko