简体   繁体   English

如何使用if else比较显示项目列表

[英]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. 在node.js应用程序视图中,如果查询数据的计数不等于零,我想显示汽车列表。 My query works fine but having issues with my if else statement. 我的查询工作正常,但我的if else语句有问题。 Below is the short code i have written in my ejs view 以下是我在ejs视图中编写的简短代码

<% 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. 当data.length> 0时,将显示正确的汽车数量,但如果data.length = 0,则不会显示将汽车添加到配置文件中的文本。 How do i fix this? 我该如何解决? What would be the right way to use javascript if else statement in ejs? 在ejs中使用JavaScript if else语句的正确方法是什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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