Hi everyone.
Now let's create a new folder.
Okay, we already have a post and in this video I will create a user.
Okay, now but, I will not do anything inside the post or sorry, inside the user module.
The reason because I will not do something like authentication or something like that inside the user.
Okay, but I just want to show you how we can communication between the another module.
Okay.
And in more detail you can check out the next project okay.
In the next project is a very big project.
In this course
So for that reason I will NOT do that in this project right now.
But anyway let me create a generate module using the user.
Okay.
After create the user, I will create a controller.
Or I'll create a service first
And also the controller.
Now we already have some stuff like this
Let me remove the some specs file.
And now let's open the userService right here.
And what we will inject right now if you open the post and open the postService, we will inject the the
post module
Right.
Sorry.
The postSchema
Right.
But
In this user we don't have any schema.
But, I want to show you how we can inject another service inside here.
Let me show you.
For example in the constructor. I will inject the postService with the PostService
Now if I save and check out. Now it say the some errors like this
If you read more about that, you can see the first one it say
The user module is a valid NestJS module?
So it means inside the app module right here.
Let's see, we already import the user module.
It means this clearly the valid NestJS module
So let's go to the next step.
If PostService is a provider, it is a part of current user module.
It means it say right now. if I open the user module right here inside, we doesn't have any thing related
to the post service inside the user module, right.
For that reason, right now we must import the post service.
Let's see, it will bring for me a new error. It say the postModel
Okay.
It say we must inject the post module.
And now.
Because.
But why?
Inside we must inject postModel.
Now you can see
Because in the provider here, we already import the PostService
Right.
We already import the PostService
And the reason?
Because the PostService it inject the PostModel
Right?
For that reason, we must open the postModel
And I will copy entire import right here
And paste it inside the user module like this, and import right here
And the Post
And make sure the Post come from the schema okay.
Not from nestjs
common
From the schema, okay, make sure from the schema, and also import the schema right here
Now let me save that.
And it's already worked right.
But you can see it a little bit silly if I do that now.
For example, inside the PostService, I inject many many service
Okay I will inject for example ten service.
It means inside user module.
Right here we must import 10 of that, right?
And for that reason it's very silly when I do that.
So the shorthand syntax I will show you.
Firstly I will not import anymore.
I will remove entire import and I also return.
I also do not need to involve the users anymore.
Let me show you what I mean.
And I want.
I only need to import the module.
The PostModule
Okay.
And let's see, the application clear
NOT work
But you can see, if I have a ability to do this, I just need to import the module.
And because the module right here is already import many services, right?
For that reason I don't need to re-import this again into the user module.
Okay.
But right now it's say the error for that reason in the post module we just need to export
PostService, because the service by default it's not export
Now let me recap what I mean.
Firstly, because the UserService inject the PostService okay, it means the user module right here
must have ability to inject the PostService
Right.
And I don't want to inject the PostService because it will have a so many thing related to the PostService, right?
For that reason I just need to import a PostModule and by default the PostService does not export
along the way
Okay.
So for that reason we must export that. It's mean
We have a some step okay.
We just remember the some steps.
The first step will be export
Export Service
Service
The second step.
Import
The entire module
Okay
Remember two steps when we want to.
Communication between the module.
Okay.
Firstly
Go to where we want to export and export the Service, because the service is not exported by default.
Okay.
So export that
And in the user module, import post module and because the post module is already exported.
PostService
And it's also import the Schema
For that reason we don't need to re-import this again because it's already packaged inside the
PostModule
Right.
So that is that for the video.
And I will see you in the next one.