0
0
NestJSframework~10 mins

Dependency injection basics in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to inject the service into the constructor.

NestJS
constructor(private readonly [1]: CatsService) {}
Drag options to blanks, or click blank then click option'
Aservice
BCatsService
CcatsService
DCats
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of a variable name.
Using a variable name that does not match the service.
2fill in blank
medium

Complete the code to mark the service as injectable.

NestJS
@[1]()
export class CatsService {}
Drag options to blanks, or click blank then click option'
AInjectable
BController
CModule
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller or @Module instead of @Injectable.
Forgetting to add the decorator.
3fill in blank
hard

Fix the error in the provider registration.

NestJS
@Module({
  providers: [[1]],
})
export class CatsModule {}
Drag options to blanks, or click blank then click option'
ACatsService()
BCatsService
CcatsService
Dnew CatsService()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the service as a function in providers.
Using an instance instead of the class.
4fill in blank
hard

Fill both blanks to inject and use the service in the controller.

NestJS
constructor(private readonly [1]: [2]) {}

getCats() {
  return this.catsService.findAll();
}
Drag options to blanks, or click blank then click option'
AcatsService
BCatsService
Ccats
DCatService
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching variable name and type.
Using incorrect service class name.
5fill in blank
hard

Fill all three blanks to create a provider with a custom value and inject it.

NestJS
const customProvider = {
  provide: '[1]',
  useValue: [2],
};

constructor(@Inject('[3]') private readonly config: any) {}
Drag options to blanks, or click blank then click option'
ACONFIG_OPTIONS
B{ apiKey: '12345' }
DCONFIG
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching the provide and inject tokens.
Using incorrect object syntax for useValue.