0

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.

1
  • 1
    what have you done so far? ie sample code Commented Apr 7, 2013 at 9:39

2 Answers 2

4

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 %>
<% }) %>
Sign up to request clarification or add additional context in comments.

Comments

0

Use EJS template url and send sql results using:

.render()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.