
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
Complete source code for this lesson is available at
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.
var
(function-scoped, outdated)let
(block-scoped, modern and recommended)const
(block-scoped, cannot be reassigned)_
, or $
let let = 5;
is invalid)myVar
and myvar
are different)string
, number
, boolean
, null
, undefined
, bigint
, symbol
Objects
, Arrays
, Functions
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
00:00:00 There's a bug with the current implementation of the vote function, right?
00:00:04 Ideally, if a user upvotes or downvotes for the first time, then a new vote should be created.
00:00:10 But if a user has already downvoted and then if they upvote, well, we shouldn't remain the number of the downvotes.
00:00:17 We should reduce it by one.
00:00:19 So head over into the vote actions, specifically where we have the createVote function.
00:00:27 I think I made a couple of mistakes in this function.
00:00:30 The first one right here is that if there's no user ID, we have to return the output of the handle error function, not just call it.
00:00:39 So that's one thing, similar to what we're doing here.
00:00:42 And then most importantly, we have to head below to where we're trying to find an existing vote.
00:00:49 So if the user has already voted with a different vote type, we have to update the vote by saying await vote.findbyid and update,
00:00:58 existing vote ID, vote type, new true, session.
00:01:03 But what is happening here is we have to update both the votes, the current one that we have to remove and the new one that we have to keep.
00:01:11 So first things first, we want to duplicate this await update vote count just below.
00:01:17 And for the first time, we want to say vote type will be equal to existing vote.type.
00:01:25 And for this one, we actually want to decrease it by one.
00:01:29 But if the vote type is new, we want to increase it by one.
00:01:33 So with that little change put in place, we can also delete the current votes that we have added to this question just to be able to test everything properly.
00:01:41 So head over to your MongoDB Atlas, or you can go right here within your application, head over to Dev Overflow, Votes, Documents,
00:01:51 and delete this document vote.
00:01:54 And same thing in questions, documents.
00:01:57 You want to hit this specific question and want to reset the number of upvotes and downvotes to zero.
00:02:05 Now, if you reload, you'll see that both of these are set to zero.
00:02:08 So if you upvoted once again, you'll see upvote.
00:02:11 If I click on it, you'll see that it gets reset to zero.
00:02:14 If I downvote, the same thing happens with the downvote.
00:02:17 But now, if I have one activated and I click on the other, oh, it still stays.
00:02:23 I think that's because I made just a tiny mistake.
00:02:26 The existing vote right here doesn't have access to a property called type.
00:02:30 It actually has a property of vote type.
00:02:34 We can check that if we go into the vote model where you can see that it has vote type.
00:02:42 TypeScript didn't save me this time, but that's okay.
00:02:46 I'll go ahead and reset the number of votes right here on this document to zero and zero.
00:02:54 And I'll remove the existing vote.
00:02:58 Once you do that, I'll be able to reset the page and try it out once again.
00:03:03 Now you can see that it gets properly reset to zero and the other one gets added.
00:03:08 So now a single user can only add either one up vote or one down vote.
00:03:15 So since everything is working, let's go ahead and push it.
00:03:18 I'll say fix vote issue.
00:03:23 Commit and sync.
00:03:26 And soon enough, we'll be ready to start adding the votes to our answers as well.
00:03:30 That matters even more than the question.
00:03:33 Well, I guess if a question is upvoted, it means it's a common issue in a specific language or technology.
00:03:38 But if an answer has many upvotes, well, then it must mean that it is a good answer.