Showing the number of likes
We keep the likes in an array. It is easy to count the elements there and find out how many times a post is liked. We will make two small changes that will make this possible. The first one is in the API, which is the place where we prepare the post objects:
// backend/api/content.js result.forEach(function(value, index, arr) { arr[index].id = ObjectId(value._id); arr[index].ownPost = user._id.toString() === ObjectId(arr[index].userId).toString(); arr[index].numberOfLikes = arr[index].likes ? arr[index].likes.length : 0; delete arr[index].userId; delete arr[index]._id; });
A new numberOfLikes
property is attached. The records did not have a likes
property in the beginning. So, we have to check whether it exists before ...
Get Node.js By Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.