user2253758
user2253758

Reputation: 13

Display sql query result in ejs file using node express

newbie here, I want to display my sql query result into my ejs file but I don't know how to.

any help will be appreciated.

Upvotes: 0

Views: 3729

Answers (2)

hexacyanide
hexacyanide

Reputation: 91619

To use ejs, the Express render engine must be set.

app.engine('html', require('ejs').renderFile);

Then you can pass variables to templates a SQL query:

connection.query('SELECT 1', function (err, rows) {
  res.render('file.html', {
    'sql': rows
  });
});

In your template, then you can use code like so:

<%= sql.property %>

and even use scripts inside tags:

<% sql.forEach(function(field){ %>
  <%= field.property %>
<% }) %>

Upvotes: 4

ram
ram

Reputation: 2343

Use EJS template url and send sql results using:

.render()

Upvotes: 0

Related Questions