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,500 courses taught by industry experts.

Rewriting the add-to-cart endpoint

Rewriting the add-to-cart endpoint

- [Instructor] Great, so now that we've seen how to convert our load cart endpoint to use MongoDB, let's do the same kind of thing with our add to cart endpoint. So here, we have the endpoint. The first thing that we're going to need to do is, just like with all of the other endpoints that we've converted, we're going to need to connect to the DB at the top, so we'll say const { db } = await connectToDB, and the next thing that we're going to need to do is, instead of this sort of difficult to read logic here that modified the in-memory cart database, right, just the object that we defined and hard-coded, we're going to use a MongoDB query, and the good news here is that all of the finding and modifying and returning the modified document here is going to be done with a single function, right? That's just a really nice feature that MongoDB provides for this situation. So here's what this is going to look like. We're going to say const updatedCart = await db.collection 'carts', and…

Contents