【问题标题】:display a message when mongoDB collection is empty当 mongoDB 集合为空时显示一条消息
【发布时间】:2022-02-10 16:20:15
【问题描述】:

我的网站上有一个通知部分,存储在我的 Mongo 数据库中。如果集合是空的,我将如何显示一条消息,比如“没有新通知”

我正在使用 EJS 在我的页面上循环这个,我的代码如下。

        <% for (let notice of notices) {%>
            <p class="lead"><%= notice.noticeText %>
            <% } %> 

【问题讨论】:

    标签: mongodb express mongoose ejs


    【解决方案1】:

    我猜你可以这样设置条件:

      <% if (!notices.length) {%>
        <p>desired text if no notice exists</p>
      <% } else { %>
        <% for (let notice of notices) {%>
            <p class="lead"><%= notice.noticeText %>
        <% } %>
      <% } %}
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多