简体   繁体   中英

Displaying result from MongoDB collection with Pug

I have a MongoDB collection and it's structure like this;

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

And i have below pug layout;

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

This is working fine and the outcome is like this;

mytitle , john, 1619

I would like to add date field as well. So my final outcome should be like this;

mytitle , john, 1619, 2020-05-20

I tried to add created field as shown below but this is not bringing any value for created. Any idea what am i missing here?

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

In your data, created is an object, not a date string. To access the date string, you'd need to do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM