03 Depedency Injection in Nest JS | Inject Service in Controller | Node JS, Javascript, Typescript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so in our last video we looked at how we can use controllers to handle a request and send a response so if you are coming to this video with without looking at the older video so what we have done so far is installed nest js and we created a quiz module and we just created this controller which accepts a request on the slash url inside quiz and it responds back with a hard-coded array for now so we were basically understanding the routing and the routing structure and how the controllers will handle them but now we are sending some data right we are sending information from the application back to the client and we would want to understand how the separation of concerns can be done so obviously our application will never send such things to the client there's no point of having an application which sends this kind of and hard coded data there will be stuff which will have some business logic so we will need a service the service is like the think tank it has all the business logic inside it so how do we create a service now there are commands but i think for now i'll give you a manual step as well so that you understand how things work without the cli as well so we by default get a service when you create a basic nest js application okay and we are now going to follow the same structure so inside quiz i have quiz dot service it is going to be a class which we are going to export okay and this should have a decorator which is injectable okay anything which has an injectable decorator nest js will know that it can be injected into some other places for example a service will be injected into a controller so that we can use the functions inside the service so we add the injectable decorator and let's just say we have one method over here which is get all quiz and it is going to send this same data from the service obviously the service we have we have added it but we haven't said anywhere that we need the service so how do we inject the service now we we add that injectable over here right so inside the controller what i can do is like if you have worked with laravel or many other frameworks actually you can define dependencies or rather inject dependencies inside the controller and what the framework does is it manages the instantiation of that particular class okay we can do it inside a function in in laravel uh or inside constructor right in java springboot you can do that even in sjs you can do the same thing so inside constructor okay i'll have a private variable which is quiz service and this is of type quiz service so this is how um messages will know you know what needs to be injected okay but now if i save right i want you to see an error which will come in okay so my app is not running start dev so npm run start there i will see an error okay and i want to show you what happens so it says nest cannot resolve dependencies for quiz controller make sure that the argument quiz service at index 0 is available in the quiz module context so which means nest is able to understand that we are trying to send an argument which is quiz module which is inside the constructor of the quiz controller but it is not able to resolve it because there is a proper way of doing it and it says the only way to handle that is let's go into the quiz module and is it no it's imports i think imports okay quiz oh i'm sorry not quiz module quiz service okay and now if i hit save okay this is not working imports i think it's not imports hold on a second let's look at here providers right so it is not imports but rather providers okay this module is dependent on this provider service and so it should not work okay and you can see that now the error is gone and if i go to postman and hit save rather send i get the same thing okay so fine so far the array which was sent from the controller is still being saved sent back but the error about injecting the quiz service is gone so if i want to ensure that i am able to send something from here i just do from the service okay and now inside the controller instead of sending this hardcoded array i will do this dot quiz service dot get all quiz and we have no error i'll go to postman hit send and you can see that the data which is now coming is from the service and the controller is now completely dependent on the service to get the data i mean send the data and it will just pass it on so i want you to understand the key concept that in this what we have done is not only created a service but we are injecting the service okay this is how the di works inside nest okay and it is important for us to do this declaration inside this decorator which says that inside the providers we have the name of the service which we are using okay so once that is done nsjs knows that this module is dependent on these providers and that provider is then injected to the controller and then it's a very simple um javascript thing all right so that's that's how dependency injection works in sjs and we will see more concepts of messages in the future videos if you like this video do click on the thumbs up icon don't forget to subscribe to my channel and yes do share your feedback about what you think about this framework and you know anything which you like about it any things which you don't like like about this framework do share your thoughts
Info
Channel: Amitav Roy
Views: 23,396
Rating: undefined out of 5
Keywords: nestjs, nest framework, nest tutorials, nest basic tutorials, node js tutorials, node framework, typescript
Id: W5W9kHa61t0
Channel Id: undefined
Length: 7min 58sec (478 seconds)
Published: Sat Feb 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.