用Pug显示MongoDB集合的结果。

问题描述 投票:0回答:1

我有一个 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

这样做很好,结果是这样的。

mytitle约翰,1619年

我也想添加日期字段。所以我的最终结果应该是这样的。

mytitleJohn, 1619, 2020-05-20

我试着添加 created 字段如下图所示,但这并没有为创建带来任何价值。任何想法,我在这里缺少什么?

a(href=entry.url, target='new')= entry.title
|, 
span.text-success=entry.author
|, 
span.text-success=entry.vote
|, 
span.text-success=entry.created
node.js mongodb mongodb-query pug
1个回答
1
投票

在您的数据中。created 是一个对象,而不是一个日期字符串。如果要访问日期字符串,你需要做的是

span.text-success= entry.created['$date']
© www.soinside.com 2019 - 2024. All rights reserved.