Ejs - html in function not showing

Hi! Can someone let me know what is wrong with this:

<%
Topic.find({}, (err, topics) => {
console.log("Topics is: "+topics)
topics.forEach(test =>{
console.log("test is "+test);
%>
<div class="container">
  <div class="border rounded">
  <h4><a href="#"><%=test.title%></a></h4>
    <p>Info about topic</p>
  <p>Created by: Username</p>
    <p>Last Post by: Username</p>
  </div>
</div>
<%})})%>

The HTML doesn’t show, whereas the console.logs work. What am I doing wrong?

For testing, could you try this?

console.log('test is ${test}');

The ’ being `.

@RiversideRocks
Adding that I get this in the logs:
test is {
_id: 5efdbc560b14390e7b56e0d9,
title: ‘This is a topic!’,
content: ‘This is the the new topic’,
user: ‘EddiesTech’
}
test is {
_id: 5efdbc560b14390e7b56e0d9,
title: ‘This is a topic!’,
content: ‘This is the the new topic’,
user: ‘EddiesTech’
}

As I said, the console.logs work, just the HTML doesn’t show on the page

Shouldn’t there be a space between the EJS tags?

<h4><a href="#"><%= test.title %></a></h4>

Or not? :sweat_smile:

Didn’t change it. I hate it when ejs does things like this :angry: :joy:

Can you make sure the div doesn’t really exist by using the DevTools to see if the element exists?

Yep. Checked inspect element. Not there.

It might not be an EJS related issue, because EJS usually results in rendering a page with the error message alone. And check logs and Dev Tools console.

@khalby786 Am I using the right tags. I’ve never understood the difference between

<%
<%=
<%-

etc.
logs and console are fine

  • <% is for normal JavaScript execution/code flow
  • <%= is for output along with escaping HTML characters
  • <%- doesn’t escape HTML and can be used to output HTML code.
1 Like

Hi everyone! I decided to give on this and instead did the mongoose find inside the server.js file instead :slight_smile:

1 Like