繁体   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