All language subtitles for 010 Updating 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

In this video, let's implement document updating. | www.downloadly.ir

And so, here, in our update tour handler, let's start by

making it an async function.

Then, or try catch block, and so I'm doing this

very quickly now, because we're already used to this

at this point.

Right?

So this goes into the try block and then

I simply get this one, here, from the catch block

of this other function.

All right, so.

Here we go.

And so now, what we need to do here

at the beginning of this try block

is to basically query for the document that

we want to update and then update it, all right?

And we can actually do that

all in one command with mongoose.

And, remember, again, that we're gonna update the tour

based on an ID, okay?

So, it's a similar situation like this one here,

where we find by ID.

And now, we can actually do something very similar,

which is find by ID and update.

So again, everything in one query.

So, let's do tour.findByID

and update.

Okay, so, what do we need to pass in here?

Well, of course, the ID, so that we can first find

the document that is to be updated.

So, just like before, that is req.params.ID,

and then the data that we actually want to change.

And that data will be in the body,

just like in the post request.

So req.body, okay?

And then, actually as a third argument,

we can also patch in some options, and we will do that.

And the first option that I want to specify is new

and set it to true.

Because this way, then the new updated document

is the one that will be returned.

And since we want to send back that updated document

to the client, we always want this method

to actually return that new document, okay?

So this way, it actually then makes sense to

await this query and then save it to a new variable.

So then simply call it tour here,

and then again, await the result of this query.

So all of these methods that we've been using on the tour

so far, so find by ID and update, or,

well, not really create, but find by ID or find,

all of these will return queries, so they are query methods

and let me actually show that to you

in the mongoose documentation.

So, up until this point, we didn't have a look at

the documentation so far.

And so, let's quickly do that.

So, on the model, which in our case is tour,

we have all these query methods, right?

So you see find, you see find one, you see find by ID,

which is just a shorthand for find one.

Then you see find by ID and update,

which is the one that we're currently using,

and which, in fact, is just a shorthand for

find one and update, where we're querying for an ID,

so similar to what we had in the last lecture, okay?

And actually we can click on any one of these,

so let's do the one that we're using currently,

and that will then really take us to the API documentation

which shows us how to really use a certain method.

So you see that we passed the ID, so the value,

off the ID to query by, we passed the object with

the data to be updated, and we passed an options object.

And down here we can actually see

the options that we can set, so remember,

we just set new to true, and so as it says here

that will then return the modified document

rather than the original.

Now, another one that we actually want to set to true

is this run validators, so that each time that we update

a certain document, then the validators that we specified

in the schema will run again, okay?

And we will talk a lot about validators and validation

a bit later in the section,

but I'm gonna leave it here for now so that we, basically,

don't forget it.

Okay?

Give it a save now, and actually I wanna go back here

just to show you how many methods

we actually have in mongoose.

So, on the model, we have all of these methods here

that you see in this list.

So for example we used create before,

we also used find.

Okay?

And so the ones that you see here in the top,

they are available right on the model,

and that, in our case, is the tour.

Now, down here you, for example also see

model.prototype.safe for example.

And this one, we actually also already used

right when we first started to work with mongoose.

So I'm not sure if you are familiar with this,

but in java script model.prototype always means

an object created from a class, or in this case,

created from a model.

And so, the save method here, refers to a save method

called on a document and not on a tour.

And remember that that's exactly what we did.

And I think I still have it here as a comment.

Yeah, right here.

So, we had a tour, this new tour variable here

which was created from the tour model,

and so this new tour here got access to the save method

because that is part of the prototype object of this class.

All right, now I don't want to bore you with the

fundamentals of java script, but it is important

in order to understand the documentation, okay?

Because at some point you will need to use documentation,

because mongoose is yute.

And I will actually teach you a lot of the stuff

that's important in there through all of this course,

but sometimes you will still need something that

I didn't show you in this course.

And so it's important that you really understand

the documentation as well, okay?

And so, again, when you see model.prototype here,

you know that the save method here

is going to be available on all of the instances

created through a model, okay?

And so, not the model itself.

So, for example, if you tried tour.save

you wouldn't be able to use it.

It would give you an error, but instead, if you tried save

on a document created through the tour,

then it would, of course, work, okay?

I hope that makes sense.

And, anyway, you see that we have a lot of other stuff,

so we have to query, which we already talked about,

because, for example, find by ID and update, or find,

or find by ID, all of these will return query objects.

And so, later on, when we're gonna implement stuff like

sorting or filtering we will then use this query object.

Okay?

So, let's go back, we kind of got off the track here

by looking at the documentation,

but, again, I think it's really important

to start looking at the documentation,

because a course like this can only take you so far,

and I hope you already know that at this point, okay?

It's really important that you're also able to

find information on your own, and I wanna show you how

to do that also in this course.

Anyways, let's now actually also send

this newly updated tour back to the client,

which, right now, we're not doing, because we have

simply this updated tour here string.

So let's get rid of that and simply send to tour.

And remember that this, in fact, is actually this.

So, the tour property is set to the tour object,

but, thanks to ES6, we no longer have to do that

when the property name has the same name of the value.

Okay, so let's get rid of that.

That makes our code look a bit better.

Okay, and let's now test it.

Back to postman, here we have the updated tour route.

And so, let's actually do it

on this same ID that we used before.

So, this one.

Then we come to the body, here, and let's change--

Well, what are we gonna change here?

Well, let's change the price to, like, 500.

So, price, 500,

send it, and indeed this gives us

the newly updated object with the price

already set to 500.

And if we now get all tours, then that, of course,

should be reflected here.

And here it is.

But now, let's say that we set it to a string.

Let's take a look at what happens, then.

Well, then we actually get an error.

And that's because we ran the validators again.

So, remember that we have--

Where is that?

Oh yeah, here.

So here we specify that the validators should be run again.

And so, right now the price is no longer a number

as it is expected, right?

So in a schema we say that the price should be a number,

but, of course, java script, or mongoose, actually,

cannot, basically, convert this string here to a number.

But that is what we expect.

So here it says kind should be number.

And so, therefor, it gives us an error,

simply because we're running the validators again.

So, let's put it back to 500 and then, of course,

it's gonna be back.

All right, now, keep in mind that we're actually

doing a patch request here.

Now, if we were doing a put request, remember,

then you would expect that the original object

would be completely replaced

with the new one that is sent in, okay?

So, in that case it would no longer work

the way we implemented here

because this one simply really updated the fields

that are different here in the body, okay?

But anyways, I think that the patch method

is actually way more useful, and so,

that's gonna be the only one that implementing here.

Cool, so we have three of our four correct operations

implemented, only one more left to go,

which is gonna be delete. | www.downloadly.ir

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