I am using node js and templates of ejs in my application, and I am trying to show a message in case the search field doesn’t match any result, I expected it will show me “Line does not exist.”, but it shows the search results instead as shown in the screenshot below:
I tried to modify the condition in the if statement <% } else if(oneresult != ''){ %>
in different ways but it doesn’t work. Below is the code:
<%- include('partials/header') -%>
<div class="container mt-5 w-50">
<h2 class="mb-4">Search a Line</h2>
<form action="/line" method="GET">
<input
type="text"
name="line"
class="form-control"
placeholder="Enter a Line"
/>
<button type="submit" class="btn btn-danger btn-block mt-3">
Search in Database
</button>
</form>
<% if(oneresult) { %>
<h4 class="text-danger my-4">Search Results:</h4>
<table class="table table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Line</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<% oneresult.forEach( oneresult=>{ %>
<tr>
<td><%= oneresult.id %></td>
<td><%= oneresult.result %></td>
<td><%= oneresult.date %></td>
</tr>
<% }) %>
</tbody>
</table>
<% } else if(oneresult != ''){ %>
<div class="alert alert-danger mt-4">Line does not exist.</div>
<% } %>
</div>
<%- include('partials/footer') -%>
Any suggestions, please?