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 - React.js Tutorial
From the course: Next.js: Creating and Hosting a Full-Stack Site
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
-
-
-
-
-
(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)
-
-
-