Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
EJS conditional rendering not working as expected

I'm trying to implement this code but it doesn't work as expected. The problem is explained below.

<% if (Object.keys(notes).length !== 0) { %>
<p class="card-text w-100" id="bookShelf">
   <div class="inline-block">
   <% notes.forEach(note => { %>
      <div class="card ml-2" style="width: 18rem;">
         <div class="card-body">
            <h5 class="card-title"><%= note.title %> </h5>
            <p class="card-text"><%= note.body %> </p>
         </div>
      </div>
   <% }); %>
   </div> 
</p>
<% } else { %>
<p class="card-text w-100" id="bookShelf" aria-placeholder="Add some notes...">
   You have not added any notes
</p>
<% } %> 

In the above code, the 'notes' object is served from the back-end and is not empty. When rendering this ejs code I get the output in such a way that the last 'card-text' paragraph element still get served to the front-end with no element. And first 'card-text' paragraph element gets displayed separately instead of enclosing the div element containing notes. In an ideal case, it is expected to display either one of the paragraph elements but not both.

Please refer to the screenshots attached for a better understanding of the problem. I already have spent too much time on this and still couldn't figure out the issue unless it is bug of some type. Any suggestion or help however small will be appreciated.

Inspect tools snap webpage sc1 webpage sc2

EDIT: Please note that it works as expected when 'notes' object is empty i.e. only the last paragraph element is displayed.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!