简体   繁体   中英

How to display item list using if else comparison

In a node.js app view, I want to display a list of cars if the count of the query data is not equal to zero. My query works fine but having issues with my if else statement. Below is the short code i have written in my ejs view

<% if (data.lenght != 0) { %>
    <% data.forEach(function(car) { %>
        <div class="card mb-3">
            <a href="/cars/<%= car.id %>">
                <div class="card-body text-center text-capitalize">
                    <div class="card-title"><%= car.brand %></div>
                        <p class="card-text text-muted"><%= car.color %> / <%= car.engine_type %></p>
                </div>
            </a>
        </div>
    <% }) %>
<% } else { %>
    <h5 class="text-center">Add a car to your profile.</h5>
<% } %>

When data.length > 0, the correct number of cars is displayed but if data.length = 0 the text Add a car to your profile is not being displayed. How do i fix this? What would be the right way to use javascript if else statement in ejs?

似乎您应该使用一个小错误:“ <%if(data.length!= 0){%>”而不是:“ <%if(data.lenght!= 0){%>”

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM