1 00:00:01,220 --> 00:00:02,890 Let's now handle the error 2 00:00:02,890 --> 00:00:06,140 that occurs when we try to create duplicate fields 3 00:00:06,140 --> 00:00:09,633 for fields that are actually supposed to be unique. 4 00:00:11,110 --> 00:00:12,210 And so, remember, 5 00:00:12,210 --> 00:00:13,940 that this is the type of error 6 00:00:13,940 --> 00:00:14,900 that we get. 7 00:00:14,900 --> 00:00:16,673 So, we tried to create a new tour 8 00:00:16,673 --> 00:00:19,000 with a name that already exists 9 00:00:19,000 --> 00:00:21,550 and the name fields is supposed to be unique 10 00:00:21,550 --> 00:00:23,670 and so we get this error. 11 00:00:23,670 --> 00:00:25,250 Now this error here doesn't have 12 00:00:25,250 --> 00:00:27,210 a name property, okay? 13 00:00:27,210 --> 00:00:29,071 And that's because, as I mentioned before, 14 00:00:29,071 --> 00:00:30,910 it is actually not an error 15 00:00:30,910 --> 00:00:32,720 that is caused by a Mongoose. 16 00:00:32,720 --> 00:00:35,000 But instead, really, by the underlying 17 00:00:35,000 --> 00:00:37,830 MongoDB driver, okay? 18 00:00:37,830 --> 00:00:39,140 And so, what we're gonna do 19 00:00:39,140 --> 00:00:40,734 to identify this error 20 00:00:40,734 --> 00:00:45,580 is use this 11,000 code here. 21 00:00:45,580 --> 00:00:46,413 All right? 22 00:00:47,970 --> 00:00:51,263 So, we have one handler for the CastError. 23 00:00:51,263 --> 00:00:54,190 Let's now create one for the case 24 00:00:54,190 --> 00:00:55,823 that we have that error.code, 25 00:00:56,837 --> 00:01:00,673 .code equal to 11,000. 26 00:01:01,640 --> 00:01:04,138 So in that case, all error should be equal 27 00:01:04,138 --> 00:01:08,833 to the error returns from handleDuplicateFields 28 00:01:10,610 --> 00:01:12,610 and again, I'm adding DB here 29 00:01:12,610 --> 00:01:15,983 just so we know that this has to do with our database. 30 00:01:16,940 --> 00:01:18,160 All right? 31 00:01:18,160 --> 00:01:22,383 And, so let's now actually then create this function. 32 00:01:29,470 --> 00:01:32,900 And now, the message that we're gonna send 33 00:01:34,900 --> 00:01:37,470 is this one so let's create it here. 34 00:01:37,470 --> 00:01:38,993 And so I want it to say, 35 00:01:43,220 --> 00:01:46,090 Duplicate field value, and then hear the name 36 00:01:46,090 --> 00:01:49,503 of that value, so let's put that to x for now. 37 00:01:50,760 --> 00:01:53,160 We will generate that a bit later. 38 00:01:53,160 --> 00:01:57,113 So please use another value. 39 00:01:58,170 --> 00:01:59,030 All right? 40 00:01:59,030 --> 00:02:01,120 And so in the case that we just saw, 41 00:02:01,120 --> 00:02:04,093 this x here should be called the Forest Hiker. 42 00:02:05,730 --> 00:02:07,410 So basically, what we're trying to get 43 00:02:07,410 --> 00:02:11,090 out of this error message string here 44 00:02:11,090 --> 00:02:13,577 is this, "The Forest Hiker." 45 00:02:14,569 --> 00:02:16,280 So that is the duplicate value 46 00:02:16,280 --> 00:02:18,040 that we want to send to the client 47 00:02:18,040 --> 00:02:20,100 so that they can now correct it 48 00:02:20,100 --> 00:02:22,040 and create another name. 49 00:02:22,040 --> 00:02:23,595 So, how can we actually extract 50 00:02:23,595 --> 00:02:26,120 this piece of the string here? 51 00:02:26,120 --> 00:02:28,520 Well, we're gonna use a regular expression 52 00:02:28,520 --> 00:02:30,640 to, basically, as I just said, 53 00:02:30,640 --> 00:02:33,170 find the text that is between quotes. 54 00:02:33,170 --> 00:02:35,520 'Cause here in this string, all we have really 55 00:02:35,520 --> 00:02:37,670 to identify this text here, 56 00:02:37,670 --> 00:02:40,460 so to find it, is these quotes. 57 00:02:40,460 --> 00:02:41,293 All right? 58 00:02:41,293 --> 00:02:43,010 And now, as you already know, 59 00:02:43,010 --> 00:02:45,670 regular expressions can be really tough. 60 00:02:45,670 --> 00:02:47,840 And so I usually just Google them 61 00:02:47,840 --> 00:02:50,273 and so let me show you how I actually do that. 62 00:02:53,280 --> 00:02:55,250 In this case, I would simply search 63 00:02:55,250 --> 00:03:00,250 for regular expression match text between quotes. 64 00:03:05,620 --> 00:03:08,660 And then usually it's right the first result. 65 00:03:08,660 --> 00:03:11,250 And then down here we have the result 66 00:03:11,250 --> 00:03:14,698 and so we can use simply this regular expression. 67 00:03:14,698 --> 00:03:17,150 Now I see that we have down here this here. 68 00:03:17,150 --> 00:03:19,270 This also works and it's easier to read. 69 00:03:19,270 --> 00:03:21,100 And it has a bunch of up votes 70 00:03:21,100 --> 00:03:23,280 and so we simply can use this one. 71 00:03:23,280 --> 00:03:25,290 So that should work as well. 72 00:03:25,290 --> 00:03:29,870 So let's copy it and let's go back to our code, 73 00:03:29,870 --> 00:03:32,493 and say that our value, 74 00:03:34,440 --> 00:03:37,663 so const value is equal to the error.ermsg, 75 00:03:40,990 --> 00:03:45,990 so remember, that that was the name of the property. 76 00:03:46,000 --> 00:03:48,820 So, errmsg. So again, that's because 77 00:03:48,820 --> 00:03:51,010 this is an error created by Mongo 78 00:03:51,010 --> 00:03:53,383 and so these is slightly different names. 79 00:03:54,410 --> 00:03:55,243 Okay? 80 00:03:55,243 --> 00:03:57,890 So we have this string and now here we want to match 81 00:03:57,890 --> 00:03:59,973 our regular expression. 82 00:04:02,140 --> 00:04:04,260 So, match, and then in JavaScript, 83 00:04:04,260 --> 00:04:07,623 a regular expression is always between two slashes. 84 00:04:08,530 --> 00:04:12,220 Then paste it here what we just got from Stack Overflow 85 00:04:14,450 --> 00:04:16,000 and let's actually take a look. 86 00:04:22,260 --> 00:04:24,600 So, we're running already in production. 87 00:04:24,600 --> 00:04:27,340 Right now we will not send back any response. 88 00:04:27,340 --> 00:04:30,203 All I want to do now is to take a look at the value. 89 00:04:31,870 --> 00:04:36,870 So, if I sent this now we get this weird response here. 90 00:04:37,870 --> 00:04:40,910 And again, that's because we're not sending any error. 91 00:04:40,910 --> 00:04:44,213 But for now I was really just interested in the value. 92 00:04:45,540 --> 00:04:46,660 And so this, what we see here 93 00:04:46,660 --> 00:04:48,980 in the log now is that value. 94 00:04:48,980 --> 00:04:51,163 And actually it is an array. 95 00:04:52,030 --> 00:04:55,830 But what we're interested in is actually this string here. 96 00:04:55,830 --> 00:04:58,550 So it did actually match all the text 97 00:04:58,550 --> 00:05:00,670 that was between strings and so, 98 00:05:00,670 --> 00:05:01,970 we can say that it worked. 99 00:05:03,680 --> 00:05:06,720 So our regular expression worked. 100 00:05:06,720 --> 00:05:08,370 Now, all we have to say is that we want 101 00:05:08,370 --> 00:05:10,970 the first element of that array. 102 00:05:10,970 --> 00:05:12,960 So, element zero. 103 00:05:12,960 --> 00:05:16,280 And now, let's actually go ahead and return 104 00:05:16,280 --> 00:05:20,590 or new AppError, so new AppError, 105 00:05:20,590 --> 00:05:22,560 paste in the message, 106 00:05:22,560 --> 00:05:25,993 and again the 400 status code for a bad request. 107 00:05:27,999 --> 00:05:29,570 And so let's now try that again. 108 00:05:29,570 --> 00:05:31,820 Again, we are still running a production 109 00:05:31,820 --> 00:05:34,550 and so that code should now run. 110 00:05:34,550 --> 00:05:37,423 And of course, we tried to replace our x. 111 00:05:40,400 --> 00:05:42,613 Of course, we now want our value here. 112 00:05:46,750 --> 00:05:49,327 And so, now we get duplicate field value, 113 00:05:49,327 --> 00:05:51,110 "The Forest Hiker." 114 00:05:51,110 --> 00:05:51,970 Perfect. 115 00:05:51,970 --> 00:05:54,140 And now, next up, in the next video 116 00:05:54,140 --> 00:05:57,330 we will then handle the last of the three errors 117 00:05:57,330 --> 00:05:59,083 which are the validation errors.