NestJS · Junior · Question 24 of 75
How does a controller get a service instance?
@Controller("users")
export class UsersController {
constructor(private readonly usersService: UsersService) {}
}- ✓Correct answer: Through constructor injection: Nest sees the parameter type and passes the registered instance
- 2It must create it with
new UsersService() - 3It imports a global variable
Explanation
The service must be a provider in this module (or exported by an imported module). Nest resolves the whole dependency graph.