May 2015
Beginner
220 pages
4h 16m
English
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 ...
Read now
Unlock full access