1
00:00:00,150 --> 00:00:00,570
Beautiful.

2
00:00:00,900 --> 00:00:02,310
We can create the user.

3
00:00:02,850 --> 00:00:06,270
So now let's keep checking off the boxes.

4
00:00:06,700 --> 00:00:11,340
And up next, I want to check whether email is already in use.

5
00:00:11,970 --> 00:00:14,670
We can do that in two places and we can do that in a schema.

6
00:00:15,000 --> 00:00:20,580
Remember the unique property and also the controller so it can use find one.

7
00:00:21,150 --> 00:00:24,090
Try to get the user with the email.

8
00:00:24,100 --> 00:00:29,130
If we can find the user, that means that there was already user and we send back the error.

9
00:00:29,700 --> 00:00:32,100
So let's go back to the user, Jess.

10
00:00:32,520 --> 00:00:33,510
Let's keep scrolling.

11
00:00:33,520 --> 00:00:37,140
Let's find the email and we can also be unique.

12
00:00:37,530 --> 00:00:37,890
Correct.

13
00:00:38,040 --> 00:00:44,220
So we go here with unique, true and remember that technically this is not a validator, it's just checking

14
00:00:44,550 --> 00:00:45,450
for indexes.

15
00:00:45,690 --> 00:00:47,110
So that's the first place where you're working.

16
00:00:47,130 --> 00:00:47,520
Do that.

17
00:00:48,030 --> 00:00:49,740
And this definitely is an option.

18
00:00:50,310 --> 00:00:54,840
It's only a little bit annoying that we have to go through the Mango's errors.

19
00:00:55,170 --> 00:01:00,570
So essentially, we'll be sending back something from our error middleware app if you don't want to

20
00:01:00,570 --> 00:01:00,960
do that.

21
00:01:01,410 --> 00:01:05,340
You can go to the controller and where we have the register.

22
00:01:05,700 --> 00:01:07,350
First, I'll pull out the email.

23
00:01:07,560 --> 00:01:09,090
I'll say const email.

24
00:01:09,570 --> 00:01:12,600
And here it is, equal to a dead body.

25
00:01:13,170 --> 00:01:19,000
And then let's try to find the user with the email and not before we do that.

26
00:01:19,020 --> 00:01:23,880
Why don't we try to register David one more time and let's see what happens?

27
00:01:24,330 --> 00:01:31,080
I have a suspicion on everything works, so sometimes MongoDB is somewhat picky that if I already have

28
00:01:31,080 --> 00:01:35,490
one user and then I add that unique one, that you might have some bugs.

29
00:01:35,490 --> 00:01:38,640
But in this case, everything works out as duplicate value and for email field.

30
00:01:38,940 --> 00:01:40,950
Please choose another value.

31
00:01:41,280 --> 00:01:43,800
So this is coming from our error handler.

32
00:01:44,130 --> 00:01:44,580
Correct.

33
00:01:45,090 --> 00:01:48,870
And what I want to do right now is set up everything in the controller.

34
00:01:49,170 --> 00:01:55,320
So we get the email we structured from that body and come up with some kind of variable.

35
00:01:55,650 --> 00:01:57,240
Marcus, I'm going to say email.

36
00:01:58,280 --> 00:02:02,030
Already exists, that is equal to await.

37
00:02:02,900 --> 00:02:05,540
And we go with user find one.

38
00:02:06,330 --> 00:02:08,900
And I will just check based on the email.

39
00:02:09,530 --> 00:02:16,400
If the email is already in use, we go with email already exist, so we set up the commission.

40
00:02:16,730 --> 00:02:23,720
And if that's the case, we throw new let's go with custom error dot and we're looking for it by request.

41
00:02:24,110 --> 00:02:27,380
And as far as the error message, we just say email.

42
00:02:28,280 --> 00:02:29,950
Already exists.

43
00:02:30,680 --> 00:02:37,850
So let's say that one, and if I head back to your postman and try to send it now, I get this message.

44
00:02:38,240 --> 00:02:39,980
So those are the two options.

45
00:02:40,520 --> 00:02:47,930
How we can check whether the email is already in years, whether we already have the user with existing

46
00:02:47,930 --> 00:02:51,110
email, one in schema and one in the controller.

