Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Let's now handle the error
that occurs when we try to create duplicate fields
for fields that are actually supposed to be unique.
And so, remember,
that this is the type of error
that we get.
So, we tried to create a new tour
with a name that already exists
and the name fields is supposed to be unique
and so we get this error.
Now this error here doesn't have
a name property, okay?
And that's because, as I mentioned before,
it is actually not an error
that is caused by a Mongoose.
But instead, really, by the underlying
MongoDB driver, okay?
And so, what we're gonna do
to identify this error
is use this 11,000 code here.
All right?
So, we have one handler for the CastError.
Let's now create one for the case
that we have that error.code,
.code equal to 11,000.
So in that case, all error should be equal
to the error returns from handleDuplicateFields
and again, I'm adding DB here
just so we know that this has to do with our database.
All right?
And, so let's now actually then create this function.
And now, the message that we're gonna send
is this one so let's create it here.
And so I want it to say,
Duplicate field value, and then hear the name
of that value, so let's put that to x for now.
We will generate that a bit later.
So please use another value.
All right?
And so in the case that we just saw,
this x here should be called the Forest Hiker.
So basically, what we're trying to get
out of this error message string here
is this, "The Forest Hiker."
So that is the duplicate value
that we want to send to the client
so that they can now correct it
and create another name.
So, how can we actually extract
this piece of the string here?
Well, we're gonna use a regular expression
to, basically, as I just said,
find the text that is between quotes.
'Cause here in this string, all we have really
to identify this text here,
so to find it, is these quotes.
All right?
And now, as you already know,
regular expressions can be really tough.
And so I usually just Google them
and so let me show you how I actually do that.
In this case, I would simply search
for regular expression match text between quotes.
And then usually it's right the first result.
And then down here we have the result
and so we can use simply this regular expression.
Now I see that we have down here this here.
This also works and it's easier to read.
And it has a bunch of up votes
and so we simply can use this one.
So that should work as well.
So let's copy it and let's go back to our code,
and say that our value,
so const value is equal to the error.ermsg,
so remember, that that was the name of the property.
So, errmsg. So again, that's because
this is an error created by Mongo
and so these is slightly different names.
Okay?
So we have this string and now here we want to match
our regular expression.
So, match, and then in JavaScript,
a regular expression is always between two slashes.
Then paste it here what we just got from Stack Overflow
and let's actually take a look.
So, we're running already in production.
Right now we will not send back any response.
All I want to do now is to take a look at the value.
So, if I sent this now we get this weird response here.
And again, that's because we're not sending any error.
But for now I was really just interested in the value.
And so this, what we see here
in the log now is that value.
And actually it is an array.
But what we're interested in is actually this string here.
So it did actually match all the text
that was between strings and so,
we can say that it worked.
So our regular expression worked.
Now, all we have to say is that we want
the first element of that array.
So, element zero.
And now, let's actually go ahead and return
or new AppError, so new AppError,
paste in the message,
and again the 400 status code for a bad request.
And so let's now try that again.
Again, we are still running a production
and so that code should now run.
And of course, we tried to replace our x.
Of course, we now want our value here.
And so, now we get duplicate field value,
"The Forest Hiker."
Perfect.
And now, next up, in the next video
we will then handle the last of the three errors
which are the validation errors.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.