From the course: Next.js: Creating and Hosting a Full-Stack Site
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Solution: Rewriting the remove-from-cart endpoint - React.js Tutorial
From the course: Next.js: Creating and Hosting a Full-Stack Site
Solution: Rewriting the remove-from-cart endpoint
(lively music) - [Instructor] All right. Well, now that you've given the challenge a try, let's take a look at my solution to this. And my solution might be a little bit different from yours, but in general, they should probably be pretty similar. So again, first thing we had to do here was connect to the database, as usual. So we'll say const ( db ) = connectToDb();, and we need the await keyword there. So let's just add that like so. And the next thing that we need to do is use a pretty similar strategy to what we did in the Add to Cart endpoint. We're just going to use that find and update one function, except with the pull operator instead of push. So here's what this is going to look like. We're going to say, and here we can remove this whole line, we're going to say const updatedCart = await db.collection('carts'). And then we're going to say findOneAndUpdate(). And here's where the criteria comes in. We first need to tell MongoDB what we're looking for. That's going to be a…
Contents
-
-
-
-
-
(Locked)
What is MongoDB?1m 42s
-
(Locked)
Setting up hosting for MongoDB4m 54s
-
(Locked)
Adding MongoDB to Next.js6m 46s
-
(Locked)
Adding data to MongoDB7m 32s
-
(Locked)
Rewriting the list endpoints7m 29s
-
(Locked)
Rewriting the load product endpoint4m 2s
-
(Locked)
Rewriting the shopping cart endpoint5m 27s
-
(Locked)
Rewriting the add-to-cart endpoint5m 8s
-
(Locked)
Challenge: Rewriting the remove-from-cart endpoint57s
-
(Locked)
Solution: Rewriting the remove-from-cart endpoint4m 2s
-
(Locked)
-
-
-