INNER CODE UNIT · JavaScript
getUserComments
ihtasham42/social-media-app · controllers/commentControllers.js:80
const getUserComments = async (req, res) => {
try {
const userId = req.params.id;
let { page, sortBy } = req.query;
if (!sortBy) sortBy = "-createdAt";
if (!page) page = 1;
let comments = await Comment.find({ commenter: userId })
.sort(sortBy)
.populate("post");
return res.json(comments);
} catch (err) {
return res.status(400).json(err.message);
}
};