- [Instructor] Okay, so we've seen how to convert
the endpoint for getting all the products
on our site over to use MongoDB.
The next thing we're going to do is convert the other two
get endpoints on our server over to use MongoDB.
And those, again, are going to be the cart items,
the one for getting all the items in a user's cart
and the one for getting information
about an individual product.
So let's start off with the one for getting
the user's cart items.
Now, for both of these endpoints
that we're about to implement, the process
for setting up and connecting the MongoDB
is going to look pretty much identical
to what we saw with our get products endpoint.
The main difference here is going to be
the actual queries that we send to MongoDB.
You'll see what I mean in a second.
Basically, what we can do is we can copy all of this,
where we get the database, and we're also
going to want to make sure to include
this client.close() thing at the end,
and we're going to paste it at the top
of our get cart items callback.
And now that we've done that, we need to make the query
to get the items that are in a user's cart.
So here's what that's going to look like.
We're going to say const user = await db.collection('users'),
and we're going to find the user whose ID
matches the userId in this URL parameter here.
So we're going to say .findOne, and we're going to use id
to find them, so we're going to say id,
we're going to set that equal to userId.
And after that, what we're going to do
is we're going to make sure this user exists,
so we're going to say if the user doesn't exist,
we're going to want to say return res.status(404),
the user doesn't exist, and then we'll just say .json,
something like "Could not find user."
Okay.
Otherwise, assuming the user does exist,
we're going to say const products = await
db.collection('products').find({}).toArray,
so we're getting all of the products from our database,
and then what we're going to do is we're going to say
const cartItemIds = user.cartItems.
Okay, so remember that when we inserted that user
into Mongo Database, we gave that user
a property called cartItems, which was an array
containing the IDs of the items that were in their cart.
So what we need to do now is we need
to actually map over all of those IDs
and convert it into the actual product
that it represents, right?
And then we're going to send back
all those products to the user.
So here's what that's going to look like.
We're going to say const cartItems = cartItemIds.map(),
we're going to use the map function here,
and we're going to map each ID to products.find(),
and we're going to find the product there
whose ID matches that ID.
And don't worry if you don't really understand
how exactly this works.
And the last thing we're going to do
is we're going to say res.status(200)
and return the cartItems that we created here to the client.
And last but not least, we just need to call client.close(),
which will close our database connection.
So that is our cart items endpoint.
Let's test this with Postman to make sure it works.
We're going to say api/users/12345,
which is the ID that we gave to the user, and then /cart,
and we're going to click Send.
And it looks like we're getting an error there,
and that is because I forgot to add
the async keyword to the callback here.
I told you that always trips me up.
So let's try it again.
It should update, we should see that the server is listening
on port 8000, so let's try making this request again.
Oops, and it looks like I made a typo here,
so let's correct that.
That should be db.collection('users'),
not db.users('users').
I'm not exactly sure what I was thinking when I typed that,
but, anyway, let's try it one more time.
And one more thing that we have to do
is we never got the userId out of the URL parameter here,
so right above here, what we're going to do is say
const { userId } = req.params,
and let's see if that's all we need to do.
And there we go.
If we send this request, it gives us all of the items
that were in the user's cart,
which, remember, were 123, 234, and 456.
So that's the cart items endpoint.
It looks like that works well enough now.
So let's switch over to the product detail endpoint
and do pretty much the same thing inside of there.
So let's scroll down to the product detail endpoint here,
and what we're going to do is copy the connection logic,
just like we did before, and paste it inside of here.
So we're going to want to make sure that we still get
the productId, and under that,
we're going to paste the connection logic.
And let's delete this product = products.find thing,
since that's still dealing with the products array
of fake data that we had up at the top of the file.
And then let's make the request
to load that product from the database.
So here's what that's going to look like.
And before I forget, let's add
the async keyword to our callback here, okay.
Then, down here, we're going to say
const products = await db.collection ('products'),
and we're going to say .find(), and we're going to find
the product with the id equal to the productId
that we passed in, so { id: productId }.
Okay, and we're going to make that findOne() here,
and let's change this to product, it should only be product.
Okay, and then the same logic applies.
If there's a product, we're going to
send that back to the client.
Otherwise, we're going to send them a 404 saying
that we couldn't find the product.
So our server's running.
Let's check and see if this endpoint works,
or if there's a few things we need to fix.
So we'll send a request to api/products/123 and click Send.
And there we go.
We see that that sends us back the correct product info.
And just one more thing that I almost forgot
is we want to put in client.close()
at the end of our callback here.