簡體   English   中英

使用 Pug 顯示 MongoDB 集合的結果

[英]Displaying result from MongoDB collection with Pug

我有一個MongoDB集合,它的結構是這樣的;

{
    "_id": "4rc4f9653fc4ff04dd7d31h6",
    "title": "my title",
    "url": "https://myurl.com",
    "author": "john",
    "created": {
        "$date": "2020-05-20T04:12:47.457Z"
    },
    "vote": 1619
}

我有以下pug布局;

block content
  h1 #{title}
  ul.list-group
    each entry, i in entries
      li.list-group-item
        a(href=entry.url, target='new')= entry.title
        |, 
        span.text-success=entry.author
        |, 
        span.text-success=entry.vote

這工作正常,結果是這樣的;

我的頭銜,約翰,1619

我也想添加日期字段。 所以我的最終結果應該是這樣的;

mytitle , 約翰, 1619, 2020-05-20

我嘗試添加created字段,如下所示,但這並沒有為 created 帶來任何價值。 知道我在這里缺少什么嗎?

a(href=entry.url, target='new')= entry.title
|, 
span.text-success=entry.author
|, 
span.text-success=entry.vote
|, 
span.text-success=entry.created

在您的數據中, created是 object,而不是日期字符串。 要訪問日期字符串,您需要執行以下操作:

span.text-success= entry.created['$date']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM