How to pass data from express to ejs

I have server.js file with query:

app.get('/tasks', function(req, res) {
  pool.query('SELECT * FROM tasks ORDER BY task_id ASC', (error, results) => {
    if (error) {
      throw error
    }
    console.log(results.rows[0]);
    res.render('pages/tasks', {data: results.rows});
  })
});

And in EJS file, where I would like to display data:

<main>
  <div>
    <h1>res: <% data %></h1>
  </div>
</main>

Unfortunately in EJS file variable data is empty while results.rows[0] in server.js file displays correct value.

How can I pass data from server.js to EJS file?

58 thoughts on “How to pass data from express to ejs”

  1. I do not know if it’s just me or if everyone else encountering problems with your website.
    It appears like some of the text in your posts are running off the screen. Can someone else
    please comment and let me know if this is happening to them
    too? This could be a problem with my browser because I’ve had this happen before.
    Appreciate it https://tadalafili.com/

    Reply

Leave a Comment