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?

Viewed 21

I am currently building a web app that has a timeline with multiple posts and wants to add date and time information in "days ago" type of format for each post.

For a single post, using "javascript-time-ago" package, I did the following way to convert the createdAT field from MongoDB:

[controller js file]

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 });
}

[view ejs file]

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

This works well when it is displaying just a single post.

However, for the timeline, I am passing multiple posts with multiple days in one object and extracting it on the ejs side as the following:

[controller js file]

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 })
}

[view ejs file]

<% 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>
<% }%>

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 as I show above?

1 Answers

After some more additional trials and errors, I updated my code using map() as the following. Not sure this is the right way but it seems like working fine.

[controller js file]

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 })
}

[view ejs file (add)]

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