如果EJS文件上没有博客文章,我正在尝试让一个占位符工作...
如果数据库中的项目都可以正常工作,但是我在获得if else语句工作时遇到了麻烦。这是我得到的:
<% if (typeof posts != undefined || posts.length >= 2) { for (var i = posts.length - 1; i > posts.length - 3; i--) { %>
<a class="post-link" href="/posts/<%= posts[i]._id %>">
<div class="post-body">
<div class="post">
<div class="post__heading">
<h2 class="heading-secondary"><%= posts[i].title %></h2>
</div>
<div class="post__content">
<p><%= posts[i].body.slice(0, 800) %>...</p>
</div>
<div class="date">
<span class="heading-secondary-date">
<%= posts[i].date %>
</span>
</div>
</div>
</div>
</a>
<% } } else { %>
<h1>There is nothing here</h1>
<% } %>
我必须做错了什么,因为EJS没有识别if else语句。
任何帮助将不胜感激。
谢谢,
raph
尝试一下,而不是其他:
<%if(typeof posts == undefined){%><h1>There is nothing here</h1><%}%>