1
00:00:00,240 --> 00:00:08,039
Not bad, not bad in our current setup, we're nicely handling duplicate values as well as our own custom

2
00:00:08,039 --> 00:00:08,500
errors.

3
00:00:08,760 --> 00:00:14,190
So now, of course, we want to deal with those validation errors, which, of course, is the case.

4
00:00:14,190 --> 00:00:21,540
For example, in the off, when I'm registering the user and I'm right away passing in the body.

5
00:00:21,720 --> 00:00:29,250
So I'm not checking for email or password or name over here, I simply let the mongoose handle that.

6
00:00:29,640 --> 00:00:37,230
And the way we do that is pretty similar to what we have or here we simply want to set up the IF statement,

7
00:00:37,450 --> 00:00:39,060
then we'll have some kind of condition.

8
00:00:39,330 --> 00:00:42,650
In this case, we're going to be looking for validation error.

9
00:00:42,930 --> 00:00:47,970
And if that is the case again, we'll use one of the nifty JavaScript methods.

10
00:00:48,420 --> 00:00:49,140
In this case.

11
00:00:49,140 --> 00:00:50,340
We're not going to be looking for keys.

12
00:00:50,620 --> 00:00:52,380
We'll be looking for the values.

13
00:00:52,650 --> 00:00:58,950
And of course, we'll send back the nice response and not just throw out let's just go back again to

14
00:00:58,950 --> 00:00:59,670
our postman.

15
00:00:59,880 --> 00:01:04,440
And I'm not going to do that in Log-in because again and here we have our custom one.

16
00:01:04,569 --> 00:01:08,640
The battle question when I want to do is go back to the registered user.

17
00:01:09,220 --> 00:01:12,930
And here I want to remove both the email and a password.

18
00:01:13,410 --> 00:01:19,440
And yes, I'm purposely removing both just so I can clearly see our functionality in action.

19
00:01:19,710 --> 00:01:23,820
And once we do that, once we send I mean, we can still see five hundred.

20
00:01:24,330 --> 00:01:26,400
So that just means internal server error.

21
00:01:26,820 --> 00:01:30,140
But the message is already more user friendly, correct?

22
00:01:30,390 --> 00:01:34,340
We already have user validation, failed password and blah blah, blah.

23
00:01:34,560 --> 00:01:39,930
So let me show you the entire object just so you can understand the functionality we're about to set

24
00:01:39,930 --> 00:01:40,800
up better.

25
00:01:41,130 --> 00:01:47,190
And technically, I don't even need to comment this one out because if I have my first return, of course,

26
00:01:47,470 --> 00:01:48,640
this is what we'll send back.

27
00:01:48,660 --> 00:01:51,570
So let me go back to the postman, send it one more time.

28
00:01:51,750 --> 00:01:57,840
And of course, this is going to be that giant object that we're getting back and the structure is following

29
00:01:58,230 --> 00:02:00,180
where we have this errors object.

30
00:02:00,450 --> 00:02:05,420
And of course, in here we'll have all the values that are missing.

31
00:02:05,700 --> 00:02:08,960
So, of course, in our case, that is password and the email.

32
00:02:09,180 --> 00:02:15,030
And this is very, very important because, of course, if you have only one, then technically you

33
00:02:15,030 --> 00:02:17,190
don't need to worry about multiple keys.

34
00:02:17,370 --> 00:02:19,010
But that's not our scenario.

35
00:02:19,260 --> 00:02:25,590
So in our case, we do need to worry about multiple keys in that object because maybe the password,

36
00:02:25,590 --> 00:02:27,450
email and name might be missing.

37
00:02:27,840 --> 00:02:35,130
And also in that object, in the error object, not only we have errors, we also have to name and essentially

38
00:02:35,130 --> 00:02:39,900
in our F block, we just want to check whether it is a validation error.

39
00:02:40,290 --> 00:02:45,270
So let's go back and I'm going to do that above the duplicate one.

40
00:02:45,270 --> 00:02:52,170
But of course, the position doesn't really matter where l say our name if it is equal to the validation

41
00:02:52,170 --> 00:02:52,410
error.

42
00:02:52,710 --> 00:02:55,590
So let's go over here with validation error.

43
00:02:55,860 --> 00:03:00,080
And if you need to copy and paste just so you don't make some silly typos.

44
00:03:00,480 --> 00:03:03,630
So essentially if you're like me and of course you can do that.

45
00:03:03,630 --> 00:03:04,770
So let's copy and paste.

46
00:03:05,100 --> 00:03:14,640
And if that is the case, then what I want to do is get all the values for the keys in the errors object.

47
00:03:15,000 --> 00:03:22,920
So notice, of course I have the password correct or I have the email and we want to do grab this errors

48
00:03:22,920 --> 00:03:31,290
one and just get the actual values of these objects because errors is an object itself then password

49
00:03:31,290 --> 00:03:33,060
and email or object.

50
00:03:33,330 --> 00:03:37,950
So I'm looking for the actual values in there and you'll see what I mean.

51
00:03:38,210 --> 00:03:46,560
Second, so if error name is equal to validation error, then let's just go with custom error and we're

52
00:03:46,560 --> 00:03:47,670
going to be looking for the message.

53
00:03:47,880 --> 00:03:54,420
And like I said, we're going to go object and values and then we'll pass in the error and errors.

54
00:03:54,810 --> 00:03:58,710
So in the error object, of course there is errors object.

55
00:03:58,980 --> 00:04:07,050
And in there I have two more keys, password and email and I want to turn them into array and then I

56
00:04:07,050 --> 00:04:08,850
want to iterate over using map.

57
00:04:08,850 --> 00:04:14,010
So since we're getting back the error, of course I can use my map method and I'm just going to call

58
00:04:14,010 --> 00:04:14,610
this item.

59
00:04:14,610 --> 00:04:18,990
So each and every item I'll call the item and then I'll say item that message.

60
00:04:19,290 --> 00:04:25,350
So here you'll notice that of course, in there, in that values, I have a message property.

61
00:04:25,710 --> 00:04:26,160
Correct.

62
00:04:26,490 --> 00:04:29,970
And then I want to access it and send it back.

63
00:04:30,330 --> 00:04:38,550
So let's say here, item that message and then we'll just join it back to the string and add a comma.

64
00:04:38,730 --> 00:04:39,900
So that's going to be our message.

65
00:04:40,200 --> 00:04:42,600
Now, as far as that is code again for Hunter.

66
00:04:42,900 --> 00:04:50,470
So let's go here with custom error and we're looking for status code and I'll be equal to four hundred.

67
00:04:50,670 --> 00:04:57,690
So once we save and once we send it back now of course, we should get a better response, but of course

68
00:04:57,690 --> 00:04:58,050
we don't.

69
00:04:58,050 --> 00:04:59,940
Because I mean, I left my.

70
00:05:00,020 --> 00:05:01,070
First response in as.

71
00:05:01,550 --> 00:05:07,100
So let me comment this sucker out and then send it here and now, of course, to have police provide

72
00:05:07,100 --> 00:05:13,490
password, please provide email, which effectively is way more user friendly than the object that we're

73
00:05:13,490 --> 00:05:14,090
sending back.

74
00:05:14,300 --> 00:05:16,550
And also notice of the status code.

75
00:05:16,760 --> 00:05:19,430
Of course, now we're sending back four hundred.

76
00:05:19,700 --> 00:05:25,490
And if you're confused about the code, just go back to the error handler and just log what you're getting

77
00:05:25,490 --> 00:05:26,030
back from.

78
00:05:26,330 --> 00:05:29,690
Object that vollers and it will make total sense.

79
00:05:29,890 --> 00:05:33,260
So essentially a map and join will make total sense.

80
00:05:33,440 --> 00:05:34,610
So let's try it over here.

81
00:05:34,880 --> 00:05:39,530
We'll go console log and then we're looking for object dot values.

82
00:05:39,740 --> 00:05:41,800
And then here we want to pass in error.

83
00:05:41,870 --> 00:05:46,090
So the arrow object and then more specifically, the error subject.

84
00:05:46,370 --> 00:05:47,510
So let's save it.

85
00:05:47,790 --> 00:05:50,410
And back in a postman, we want to send it.

86
00:05:50,410 --> 00:05:56,420
Of course response doesn't change, but in my terminal I can clearly see that what I'm getting back

87
00:05:56,420 --> 00:06:05,690
from object dot values is array of object and then iterating over that array and then more specifically

88
00:06:05,960 --> 00:06:11,960
in each object I'm accessing the message and then I just join all of them together.

89
00:06:11,960 --> 00:06:16,200
So I set it back to the string and then I just add comma in between.

90
00:06:16,490 --> 00:06:18,420
And once we have all of this in place.

91
00:06:18,860 --> 00:06:24,470
Now, of course, we can worry about our third instance and that's going to be the cast error.

