Male Instructor: Hey, there!
So let's now learn how to read documents with Mongoose
in order to implement or get tour and get all tour's
route handlers.
And starting with the getAllTours handler.
So first off, let's delete this piece of code here.
And now what we're gonna do
in order to get all the tours from the database
is to use Tour.
So the model again.
At the end on there, we use the find method.
All right.
So remember from the MongoDB intersection,
where when we wanted to query for all the documents,
we simply used find, okay?
And so here, it's the exact same method.
And again, when we don't pass anything into it,
so just like we're doing here,
then it will return all the documents
in that collection, okay?
So in this case, in the tour collection obviously.
Now just like before, this is gonna return a promise
that we will await.
And since we wanna await,
we need to make this an async function.
So async.
Then here we can await, and save the result into tours.
All right.
And that's actually it.
So, let's get rid of these comments here.
We also don't want the requestedAt property.
So that was just for testing before.
And so, yeah, I think that's it.
So this is how we get all the tours
from the Tour collection.
So using find, then awaiting that result,
and then sending it down here.
So remember that we have this data property here
to basically envelope the tours.
And then we also send this property here,
which simply measures the number of results
that are in the tours, all right.
Okay?
Because of course, this find method here
will return an array of all these documents,
and will also very nicely convert them
into JavaScript objects, okay?
So this function here, this find method,
really does all the work for us.
Now just like before,
one more step before we can actually try this out,
which is to wrap all of this here in the try block
and then catch any potential errors.
Now in this case,
there are not gonna be any validation errors like before.
But let's still send an error here, in this case,
and let's just make it a 404.
And it doesn't really matter so much at this point
because again, we are gonna implement
some better error handling here
in the next, or in one of the next, sections.
All right?
So let's just set it here to status: 'fail',
and the message to whatever error that we might get here.
Okay.
So, let's test it out now.
It's this one, Get All Tours.
And, yup, it works.
So here we have the array of the three tours
that we currently have in our database.
Perfect.
So next up, let's implement the getTour handler.
So let's write the next one that we have here.
And let's get rid of this code,
and this code.
All right.
And this one is actually gonna work in a very similar way.
So I'm still doing this try catch here.
So error, like this.
Because we're still using async await in this one.
So async.
Because now in order to find one tour,
we are still going to be awaiting Tour.find.
But it's not gonna be just find,
but instead we're gonna use findById, all right.
So findById, but where is that ID actually coming from?
Remember that?
So let's actually take a look at the route.
So going back here to Postman,
this is how we call this end point,
oh, let's actually put a more real ID here already.
So, for example, this one
from The Park Camper.
And so our request might come from a route like this.
And so here, we have this ID parameter
right in our route, right?
So let's take a look at our router here.
And so remember that this is how we specify it,
the ID in the URL.
So this ID is gonna be part of the request obviously.
And so how do we actually get access to that ID here,
and this handler?
Well, we do req.params,id.
And it is ID
because that's what we called this variable here, right?
If it was name here,
then here we had req.params.name, right?
Remember that?
And now, let's save that to tour.
Now all right.
Then let's get this code here back.
Copy it here.
And then also this one for the error block.
And don't worry, I'm gonna explain this function here
in a second.
I just wanna finish this code here first.
So this piece of code here is gonna be exactly the same
for this catch block.
Give it a save now,
and so we get no errors here from eslint.
Now about this find ID method here,
well, remember how up here we just used find, okay?
So find is to find all of the documents.
And then we also have findOne
to find only one document, okay?
And so this findById is really just a shorthand,
so a helper function for,
let me write it here as a comment,
for writing findOne,
and then with the filter object,
which is exactly like we learned
in the MongoDB intersection.
So we have the ID field.
And then we could set that to req.params.id.
So this here would work the exact same way as this, okay?
So _id, because that is what the ID looks like.
So MongoDB is called _id.
And we could of course query for that field, right?
We did exactly that in the last section.
So we specified this filter object here,
and then the property that we're searching for,
and then the value that we want to search for.
So exactly the same again, as we did in the last section.
And this findOne method here
will then only return one of the documents.
Now this findById, as I mentioned before,
is simply a shorthand for having to write this.
So behind the scenes, it's gonna do exactly this.
But Mongoose simply wants to make our life easier.
And therefore, gives us access to an easier method
called findById,
which simply expects the ID that we're looking for.
All right?
So let me actually leave that here, just as a reference.
And so now, this would actually work already.
So, let's take a look at that.
So we already put the ID of this tour.
I think it was this Park Camper.
So ac2.
And ac2, that should be The Park Camper,
and indeed, it is.
Great.
And that is the easiest way of finding
or of querying documents from the database.
There is a ton more that we can do with queries,
and we're gonna do all of that
once we start implementing something like filtering
or sorting or pagination,
and all that good stuff a bit later in this section,
so in a couple of videos from now.
At this point though,
it's enough to simply return all of the documents here
in this route handler.
And then here, simply the one for the matching ID.
And that works now,
and so let's move on to the next one.