Reputation: 13
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
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