Exploring the Issue:
As I work on developing a liking system similar to Facebook's, where each user can like a post only once, I have encountered a problem that needs attention. In this system, I am using Mongoose and Express on the server-side, along with Next.js (React) on the client-side.
The Challenge at Hand:
One of the issues I face is related to users spamming the LIKE button. When users click the like button multiple times rapidly before it has a chance to re-render to UNLIKE, the express LIKE api may be triggered several times, resulting in an inaccurate count of "totalLikes". For instance, if a user clicks like three times quickly, the totalLikes end up showing 3 instead of 1. This behavior could potentially lead to incorrect data being stored in the database, impacting the overall functionality of the system.
Proposed Solution Strategy:
To address this issue, my plan is simple - increment totalLikes by 1 when the like button is pressed and decrement it by 1 when the unlike button is pressed. However, ensuring the proper execution of this logic without counting duplicate likes or unlikes poses a significant challenge.
router.patch(
'/like/:post',
(req, res, next) => {
// Logic here for handling Post update and User update
})
router.delete(
'/like/:post',
// Similar logic for handling unlike operation
)
Critical Questions to Consider:
- What effective measures can be implemented on the server-side to prevent such occurrences?
- Is there a reliable way to regulate client-side interaction to avoid multiple firing actions, such as disabling the React button upon a click event?
- Given my limited experience as a backend developer, are there any recommendations or enhancements to improve the existing Pseudo code?