Like / Dislike System with MongoDb

I’m working on a like/dislike system on an API that works with mongoDB and nodejs and the user is supposed to be able to perform these actions:

  • Like the sauce once ( like = 1) and add the user id in a “usersLiked” array
  • Cancel his like ( like = 0) and remove the user id from the “usersLiked” array
  • Dislike the sauce once (like = -1) and add the user id in an array “usersDisliked”
  • Cancel his dislike (like = 0) and supposedly removed the user id from the “usersDisliked” array

The logic of the system like / dislike

I managed to make the user like the sauce, cancel his like and dislike the sauce.

But I am completely blocked on the dislike cancellation because when I try to use again the updateOne method in the condition where we have to cancel the like / dislike I have this error:

Cannot set headers after they are sent to the client

How could Improve the code ?

I’m learning little by little to use the backend commands and here I’m facing a wall.