All language subtitles for 012 Handling Mongoose Validation Errors_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
fa Persian Download
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

Finally, let's now handle

Mongoose's validation errors.

So remember how we tried to update a tour

with some invalid data here,

and then got this kind of error?

Okay.

And let's actually try to add another one.

So a name that's really short, okay?

And because that's not allowed, again.

Oh and now we actually no longer can see our error

because we're now in production.

And of course this error right now

is not handled correctly.

It is not marked as operational.

And so therefore,

remember we get this kind of generic error message back.

Okay?

So just to see the error that we had before,

let's quickly switch back to development.

So npm start.

And,

send it now again.

And so now we get back our development errors, okay?

Because now I want to show you

how we can actually create a meaningful error message

based on all of this, what we have here.

Now right.

So inside our error,

we get an error properties.

And that property itself is an object

which has a lot of objects in there,

and each of them is for one of the fields that has an error.

All right?

So the first one here is for the length of the tour.

Then the second one is for the difficulty,

that is also wrong.

And the third one is for the rating, okay?

So each of these actually has a nice error message, okay?

So basically the one that we defined in our Mongoose schema.

And so now we want to extract

these three messages from here,

and put them all into one string, all right?

So let's go ahead and do that.

Okay.

And again, I'm gonna start

by actually creating the conditional down here.

So if(error),

and actually let's take a look.

So here we have the error.

We have the errors, all of them.

And I need to scroll here.

And yeah, so here's the name.

So error.name is ValidationError, all right?

So let's grab that.

Okay.

And so this again, is an error created by Mongoose.

So just like the first one,

and so they look similar, okay.

Now I don't want this here.

But instead I want that the error

should be equal to handleValidationErrorDB,

and send in the error, okay?

Now let's copy this again.

All right.

And let's again, simply start by creating our message.

Invalid input data.

And then let's also return the error.

So new AppError(message, 400).

So VS Code already recognized

that I wanted to type just that, all right?

Now in order to create one big string

out of all the strings from all the errors,

we basically have to loop over all of these objects,

and then extract all the error messages into a new array.

So let's again, take a look at that.

Okay.

So the object that has all of the objects

in there is errors, okay?

So we have one error for name, one for difficulty,

and one for ratingsAverage.

And so we're gonna basically loop over this errors object.

Okay?

And in JavaScript, we use Object.values

in order to basically loop over an object.

So the elements of an object.

All right?

So let's create a variable here called errors,

which again will be an array

of all the error messages for now,

and now Object.values.

And so we want the values of err.errors, all right?

And now loop over them using a map.

And then in each iteration,

we are simply gonna return the error message, okay?

So just to make sure we're all on the same page here,

the Object.values are these objects, okay?

So this object, and this object, and the next one, okay?

So these are the values.

And so now all we have to do

in order to extract the message,

is to say value.message, okay?

So .message.

So basically the current value,

or let's say the current element,

I like to use element for that,

and we want to return el.message.

Okay.

And now, of course, this should not be here.

And you had probably already noticed that.

So in fact, this is where we want this, okay?

And so now all we need to do

is to pluck this into our message string, all right?

So errors, and now we simply join all of them together

into one string using period and then space, okay?

And you will see, in a second, why that is.

All right.

So let's switch back to production here.

So running our production start script.

Try it again now and let's wait for it,

and bam!

Here we go!

So invalid input data.

Then the first error string,

must have more or equal than 10 characters.

Then the second one,

and the third one.

Perfect.

And so that's why I used the period and space, okay?

So to basically separate these three strings

with a period and a space, all right?

And now this looks like a very nicely formatted

error message that everyone can easily understand.

Right?

So, we're basically done here.

All right.

Now we could've made this error, handling error,

a lot more complete still.

For example, we could define different error severity levels

like saying, this error is not so important,

this error is medium important,

and this error is very important or even critical.

And we could also then email some administrator

about critical errors.

And really, there's a lot of stuff that we could implement.

But again, in a kind of small application like this one,

what we have here is already really good, okay?

So this is quite a robust strategy already

that we have implemented here,

and I'm really happy with it, okay?

So all this logic here with the operational errors

that we implemented here,

so that's already quite sophisticated.

Okay?

Now if we were ever to find another error

that we want to mark as operational,

then of course all we would have to do

is something similar to what we have here, okay?

So basically implement another function for that one,

and then return our own operational error

so that the send error production

can then actually send that operational error

to the client, right?

Okay, and with that being said,

our error controller is actually finished.

But there are still some other errors that we need to handle

which are completely outside of Mongo or even of Express.

And so we're doing that in the rest of this section.

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