INNER CODE UNIT · JavaScript
getPostComments
ihtasham42/social-media-app · controllers/commentControllers.js:48
const getPostComments = async (req, res) => {
try {
const postId = req.params.id;
const comments = await Comment.find({ post: postId })
.populate("commenter", "-password")
.sort("-createdAt");
let commentParents = {};
let rootComments = [];
for (let i = 0; i < comments.length; i++) {
let comment = comments[i];
commentParents[comment._id] = comment;
}
for (let i = 0; i < comments.length; i++) {
const comment = comments[i];