I have a 'followed' section on my website and it retrives the posts of all users the user has followed. Is there a way of getting these sorted by date?
here's the code so far:
exports.followedPosts = async (req, res) => {
try {
const user = await User.findById(req.user._id);
const posts = [];
for (const follow of user.follows) {
const partPosts = await Post.find({ author: follow.user })
.select('-comments')
.populate('author')
.exec();
for (const post of partPosts) {
posts.push(post);
}
}
res.send(posts);
} catch (err) {
console.error(err.message);
res.status(500).send('server error');
}
};