簡體   English   中英

當在一個 object 中傳遞多天的多個帖子時,有沒有辦法以“前一天”的格式從 MongoDB 轉換 createdAt?

[英]Is there a way to convert createdAt from MongoDB in the format of "day ago" when passing multiple posts with multiple days in one object?

我目前正在構建一個 web 應用程序,該應用程序具有包含多個帖子的時間線,並希望為每個帖子添加“幾天前”格式的日期和時間信息。

對於單個帖子,使用“javascript-time-ago”package,我執行以下方法將 createdAT 字段從 MongoDB 轉換為:

[控制器js文件]

const TimeAgo = require('javascript-time-ago');
const en = require('javascript-time-ago/locale/en');
TimeAgo.addDefaultLocale(en)
const timeAgo = new TimeAgo('en-US')

module.exports.showPost = async (req, res,) => {
    const post = await Post.findById(req.params.id).populate({
        path: 'reviews',
        populate: {
            path: 'author'
        }
    }).populate('author');
    if (!post) {
        req.flash('error', 'Cannot find that post!');
        return res.redirect('/posts');
    }
    const user = await User.findById(post.author); 
    const created = post.createdAt;
    const createdAgo = timeAgo.format(created);

    res.render('posts/show', { post, user, imgLocation, createdAgo });
}

[查看ejs文件]

<div class="card-footer text-muted">
    posted: <%= createdAgo %>
</div>

當它只顯示一個帖子時,這很有效。

但是,對於時間線,我在一個 object 中傳遞了多天的多個帖子,並在 ejs 端將其提取如下:

[控制器js文件]

module.exports.index = async (req, res) => {
    const posts = await Post.find({}).populate().populate('author');
    const currentUser = await User.findById(req.user);
    res.render('posts/index', { posts, currentUser, imgLocation })
}

[查看ejs文件]

<% for (let post of posts){%>
    <div class="card mb-3">
        <div class="row">
           <div class="col-md-4">
               <% if(post.images.length) { %>
                   <img class="img-fluid" alt="" src="<%= post.images[0].url %>">
               <% } else { %>
                   <img class="img-fluid" alt="" 
                                src="<%= imgLocation %>">
               <% } %>
           </div>
        </div>
    </div>
<% }%>

如上所示,當在一個 object 中傳遞多天的多個帖子時,有沒有辦法以“前一天”的格式從 MongoDB 轉換 createdAt?

經過一些額外的試驗和錯誤之后,我使用 map() 更新了我的代碼,如下所示。 不確定這是正確的方法,但似乎工作正常。

[控制器js文件]

const TimeAgo = require('javascript-time-ago');
const en = require('javascript-time-ago/locale/en');
TimeAgo.addDefaultLocale(en)
const timeAgo = new TimeAgo('en-US')

module.exports.index = async (req, res) => {
    let posts = await Post.find({}).populate().populate('author');
    const currentUser = await User.findById(req.user);
    posts = posts.map(function(currentObject){
        return {
            _id: currentObject._id,
            title: currentObject.title,
            images: currentObject.images,
            description: currentObject.description,
            author: currentObject.author,
            reviews: currentObject.reviews,
            createdAt: timeAgo.format(currentObject.createdAt),
            updatedAt: currentObject.updatedAt
        }
    })
    res.render('posts/index', { posts, currentUser, imgLocation })
}

[查看ejs文件(添加)]

<p class="text-muted">
    posted: <%= post.createdAt %>
</p>

暫無
暫無

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

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