I have created a website that lets users create and add events, now I have added a search so that users can search through different events, the problem is when the event searched is not present, it should log an error message saying "No events found" but this message is displayed multiple times depending on how many events are currently present instead of showing it only once because of the forEach()
method. What should I do to make it work and display the error message only once, if no event is found with that name.
search.ejs
<div class="container">
<header class="jumbotron" style="padding-left: 0px; padding-top: 5px; padding-bottom: 5px;">
<h2>Search Results:</h2>
</header>
<!-- <% console.log(search_event)%> -->
<div class="row text-center">
<% events.forEach(function(i){ %>
<% if(i.name.includes(search_event)) { %>
<div class="col-md-3 col-sm-6"> <!--Double cols to make the site responsive and mobile compatible-->
<div class="thumbnail">
<img src=" <%= i.image %>">
<div class="caption">
<h4> <%= i.name%> </h4>
</div>
<p>
<a href="/events/<%= i._id %>" class="btn btn-primary">More Info</a>
</p>
</div>
</div>
<% } else { %>
<h4> No such event exists </h4>
<% } %>
<% });%>
</div>
</div>
<% include partials/footer %>