From ad5123f10aa24e1759402aa2fd55543a60593866 Mon Sep 17 00:00:00 2001 From: ak Date: Thu, 28 Sep 2023 14:18:24 -0700 Subject: [PATCH] updated comments to return in desc order by date --- controllers/comment.js | 4 +--- controllers/post.js | 8 +++++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/controllers/comment.js b/controllers/comment.js index 385501d..3b41af0 100644 --- a/controllers/comment.js +++ b/controllers/comment.js @@ -47,9 +47,7 @@ exports.post = [ // returns comment in json format - R exports.get = asyncHandler(async (req, res, next) => { - const comment = await Comment.findOne({ _id: req.params.commentID }) - .lean() - .exec(); + const comment = await Comment.findOneById(req.params.commentID).lean().exec(); return res.status(200).json({ comment }); }); diff --git a/controllers/post.js b/controllers/post.js index d9ba4f0..840c55e 100644 --- a/controllers/post.js +++ b/controllers/post.js @@ -95,7 +95,13 @@ exports.post = [ // returns post in json format - R exports.get = asyncHandler(async (req, res, next) => { const dbPost = await Post.findById(req.params.postID).lean().exec(); - const comments = await Comment.find({ post: dbPost._id }); + const comments = await Comment.find({ post: dbPost._id }) + .sort({ + field: "date", + desc, + }) + .lean() + .exec(); const post = { title: dbPost.title, date: dbPost.date,