WEBVTT

1
00:00.650 --> 00:02.240
Hi everyone.

2
00:02.270 --> 00:05.390
Now let's create a new folder.

3
00:05.480 --> 00:10.610
Okay, we already have a post and in this video I will create a user.

4
00:11.300 --> 00:19.040
Okay, now but, I will not do anything inside the post or sorry, inside the user module.

5
00:19.910 --> 00:27.170
The reason because I will not do something like authentication or something like that inside the user.

6
00:27.200 --> 00:35.180
Okay, but I just want to show you how we can communication between the another module.

7
00:35.570 --> 00:36.110
Okay.

8
00:37.520 --> 00:42.950
And in more detail you can check out the next project okay.

9
00:42.980 --> 00:45.800
In the next project is a very big project.

10
00:45.830 --> 00:46.550
In this course

11
00:46.760 --> 00:51.320
So for that reason I will NOT do that in this project right now.

12
00:51.440 --> 00:56.930
But anyway let me create a generate module using the user.

13
01:00.080 --> 01:00.470
Okay.

14
01:00.890 --> 01:03.830
After create the user, I will create a controller.

15
01:05.810 --> 01:07.370
Or I'll create a service first

16
01:11.480 --> 01:13.670
And also the controller.

17
01:18.470 --> 01:23.180
Now we already have some stuff like this

18
01:23.630 --> 01:25.310
Let me remove the some specs file.

19
01:26.330 --> 01:28.790
And now let's open the userService right here.

20
01:29.180 --> 01:37.280
And what we will inject right now if you open the post and open the postService, we will inject the the

21
01:37.310 --> 01:38.150
post module

22
01:38.180 --> 01:38.810
Right.

23
01:41.000 --> 01:41.420
Sorry.

24
01:41.420 --> 01:43.100
The postSchema

25
01:43.130 --> 01:43.580
Right.

26
01:44.240 --> 01:44.840
But

27
01:45.530 --> 01:48.320
In this user we don't have any schema.

28
01:48.350 --> 01:54.920
But, I want to show you how we can inject another service inside here.

29
01:55.310 --> 01:56.060
Let me show you.

30
01:56.090 --> 02:01.880
For example in the constructor. I will inject the postService with the PostService

31
02:04.200 --> 02:14.190
Now if I save and check out. Now it say the some errors like this

32
02:14.220 --> 02:18.270
If you read more about that, you can see the first one it say

33
02:18.270 --> 02:21.810
The user module is a valid NestJS module?

34
02:22.320 --> 02:24.810
So it means inside the app module right here.

35
02:24.840 --> 02:29.700
Let's see, we already import the user module.

36
02:29.730 --> 02:33.720
It means this clearly the valid NestJS module

37
02:33.750 --> 02:36.000
So let's go to the next step.

38
02:37.080 --> 02:43.260
If PostService is a provider, it is a part of current user module.

39
02:43.260 --> 02:53.790
It means it say right now. if I open the user module right here inside, we doesn't have any thing related

40
02:53.790 --> 02:57.090
to the post service inside the user module, right.

41
02:57.120 --> 03:01.740
For that reason, right now we must import the post service.

42
03:07.170 --> 03:14.100
Let's see, it will bring for me a new error. It say the postModel

43
03:14.130 --> 03:14.670
Okay.

44
03:14.700 --> 03:18.420
It say we must inject the post module.

45
03:18.600 --> 03:20.430
And now.

46
03:21.510 --> 03:22.230
Because.

47
03:23.100 --> 03:23.910
But why?

48
03:23.940 --> 03:26.880
Inside we must inject postModel.

49
03:26.910 --> 03:28.050
Now you can see

50
03:28.080 --> 03:32.940
Because in the provider here, we already import the PostService

51
03:32.970 --> 03:33.420
Right.

52
03:33.420 --> 03:34.830
We already import the PostService

53
03:34.950 --> 03:36.450
And the reason?

54
03:37.290 --> 03:40.380
Because the PostService it inject the PostModel

55
03:40.410 --> 03:41.250
Right?

56
03:41.550 --> 03:44.730
For that reason, we must open the postModel

57
03:44.730 --> 03:48.060
And I will copy entire import right here

58
03:48.990 --> 03:55.440
And paste it inside the user module like this, and import right here

59
03:55.860 --> 03:56.640
And the Post

60
03:57.300 --> 04:01.440
And make sure the Post come from the schema okay.

61
04:02.010 --> 04:03.150
Not from nestjs

62
04:03.180 --> 04:03.840
common

63
04:05.890 --> 04:12.250
From the schema, okay, make sure from the schema, and also import the schema right here

64
04:13.180 --> 04:14.770
Now let me save that.

65
04:18.220 --> 04:20.650
And it's already worked right.

66
04:20.680 --> 04:27.070
But you can see it a little bit silly if I do that now.

67
04:27.070 --> 04:32.770
For example, inside the PostService, I inject many many service

68
04:32.980 --> 04:35.920
Okay I will inject for example ten service.

69
04:36.730 --> 04:41.590
It means inside user module.

70
04:41.590 --> 04:47.110
Right here we must import 10 of that, right?

71
04:50.500 --> 04:56.170
And for that reason it's very silly when I do that.

72
04:57.070 --> 05:01.660
So the shorthand syntax I will show you.

73
05:01.690 --> 05:04.660
Firstly I will not import anymore.

74
05:04.690 --> 05:09.800
I will remove entire import and I also return.

75
05:10.220 --> 05:16.400
I also do not need to involve the users anymore.

76
05:16.910 --> 05:18.950
Let me show you what I mean.

77
05:19.460 --> 05:21.350
And I want.

78
05:21.950 --> 05:25.160
I only need to import the module.

79
05:26.600 --> 05:27.470
The PostModule

80
05:29.780 --> 05:30.320
Okay.

81
05:32.270 --> 05:35.060
And let's see, the application clear

82
05:35.090 --> 05:35.810
NOT work

83
05:37.250 --> 05:46.460
But you can see, if I have a ability to do this, I just need to import the module.

84
05:46.460 --> 05:53.330
And because the module right here is already import many services, right?

85
05:53.390 --> 06:00.890
For that reason I don't need to re-import this again into the user module.

86
06:01.010 --> 06:01.520
Okay.

87
06:01.550 --> 06:10.730
But right now it's say the error for that reason in the post module we just need to export

88
06:10.850 --> 06:14.750
PostService, because the service by default it's not export

89
06:16.280 --> 06:18.740
Now let me recap what I mean.

90
06:18.770 --> 06:27.050
Firstly, because the UserService inject the PostService okay, it means the user module right here

91
06:27.080 --> 06:31.550
must have ability to inject the PostService

92
06:31.550 --> 06:32.090
Right.

93
06:33.710 --> 06:42.950
And I don't want to inject the PostService because it will have a so many thing related to the PostService, right?

94
06:42.980 --> 06:52.790
For that reason I just need to import a PostModule and by default the PostService does not export

95
06:52.820 --> 06:53.690
along the way

96
06:54.020 --> 06:54.470
Okay.

97
06:54.500 --> 06:58.370
So for that reason we must export that. It's mean

98
06:58.460 --> 07:00.260
We have a some step okay.

99
07:00.290 --> 07:03.410
We just remember the some steps.

100
07:03.800 --> 07:07.160
The first step will be export

101
07:09.110 --> 07:11.750
Export Service

102
07:11.780 --> 07:13.100
Service

103
07:14.480 --> 07:15.290
The second step.

104
07:16.430 --> 07:16.940
Import

105
07:17.120 --> 07:19.130
The entire module

106
07:20.540 --> 07:20.930
Okay

107
07:21.170 --> 07:24.200
Remember two steps when we want to.

108
07:24.230 --> 07:27.290
Communication between the module.

109
07:27.320 --> 07:27.950
Okay.

110
07:28.190 --> 07:28.820
Firstly

111
07:30.980 --> 07:39.710
Go to where we want to export and export the Service, because the service is not exported by default.

112
07:40.160 --> 07:40.550
Okay.

113
07:41.420 --> 07:43.490
So export that

114
07:44.300 --> 07:50.690
And in the user module, import post module and because the post module is already exported.

115
07:50.900 --> 07:51.440
PostService

116
07:51.440 --> 07:54.740
And it's also import the Schema

117
07:54.830 --> 08:04.340
For that reason we don't need to re-import this again because it's already packaged inside the

118
08:04.430 --> 08:05.480
PostModule

119
08:05.510 --> 08:05.990
Right.

120
08:06.230 --> 08:08.630
So that is that for the video.

121
08:09.260 --> 08:11.210
And I will see you in the next one.