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

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