gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:47:01 25 4
gpt4 key购买 nike

在 node.js 应用程序 View 中,如果查询数据的计数不等于零,我想显示汽车列表。我的查询工作正常,但我的 if else 语句有问题。下面是我在 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>
<% } %>

当 data.length > 0 时,会显示正确的汽车数量,但如果 data.length = 0,则不会显示文本“将汽车添加到您的个人资料”。我该如何解决?在 ejs 中使用 javascript if else 语句的正确方法是什么?

最佳答案

看来你有一个小错误,你应该使用:"<% if (data.length != 0) { %>"代替 :“<% if (data.lenght != 0) { %>”

关于javascript - 如何使用 if else 比较显示项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48062410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com