All language subtitles for 009 Reading Documents_Downloadly.ir_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

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.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.