01/12/2020 — ‘N-ice!’ posts and Deleting posts
So that users can appreciate each others posts and delete posts that they no longer wish to share, we thought it would be interesting to see how we could integrate this functionality in to our app.
‘N-ice!’ posts
We began by creating a ‘Like’ function in our posts.js controller file:
Our ‘like’ function needs to firstly check and log the user that liked the post, and secondly, update the number of likes on that post. Javascript cannot do two things at once so we created two Promise functions to ensure that both functions are completed before finally displaying the outcome.
Deleting posts
This function was a little less complicated to implement. We added the delete function to our profile.js controller file as we only wanted to allow users to have the ability to delete their own posts.
The .deleteOne method in Mongoose allows us to search the database for a particular post and remove that data from the relevant field so it will no longer be displayed on the webpage:
This gives the user the ability to delete a post in their profile page: