I have a ejs page where I am trying to use the following code:
<% dbcon.getProducts().then(results => { %>
<% }).catch(err => { console.log(err) }) %>
However, these %><% don't seem to be working. While the ones in the front and the end are blue, the ones in between are grey like they aren't working. The reason I need %><% is so that I could add code in between. However, it doesn't seem to work. I don't know what's wrong and I really need it to work. Also, if there is a better way of doing this I will appreciate any help.
Update: Basically, What I am trying to do is display a database table to an ejs page. This is my ejs page:
store.ejs:
<!DOCTYPE html>
<html>
<head>
<title>PC Store | Store</title>
<% include partials/header %>
</head>
<body>
<header>
<% include partials/navbar %>
</header>
<main>
<% dbcon.getProducts().then(results => { %>
<div class="container">
<% for (var i = 0; i < results.length; i++) { %>
<h1><%= results[0] %></h1>
<% } %>
</div>
<% }).catch(err => { console.log(err) }) %>
</main>
<footer>
<% include partials/footer %>
</footer>
</body>
</html>
The code inside the doesn't want to work.
If I where to use console.log(results[i]);
it would display the database content in the console. However, it doesn't display it on the ejs web page. Also, the %><% before the <div>
and after the </div>
don't seem to work.
EJS is only a simple templating language that lets you generate HTML markup with plain JavaScript.
You should probably do the getProducts()
before and pass the result into the EJS:
js
const ejs = require('ejs');
const template = require('./template.ejs');
const results = await dbcon.getProducts();
const html = ejs.render(template , { results });
template.ejs
<% results.map(result => result) %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.