【问题标题】:Empty mongo db and EJS (Express blog app)空的 mongo db 和 EJS(Express 博客应用程序)
【发布时间】:2018-09-11 02:29:10
【问题描述】:

如果 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 语句。

任何帮助将不胜感激。

谢谢,

拉夫

【问题讨论】:

    标签: node.js mongodb express mongoose ejs


    【解决方案1】:

    试试这个而不是别的:

    <%if(typeof posts == undefined){%><h1>There is nothing here</h1><%}%>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-14
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多