0
0
NestJSframework~10 mins

Injectable decorator 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 make the service class injectable.

NestJS
import { [1] } from '@nestjs/common';

@[1]()
export class MyService {
  getHello(): string {
    return 'Hello World!';
  }
}
Drag options to blanks, or click blank then click option'
AController
BInjectable
CModule
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller or @Module instead of @Injectable.
Forgetting to import the decorator.
2fill in blank
medium

Complete the code to inject the service into the controller constructor.

NestJS
import { Controller } from '@nestjs/common';
import { MyService } from './my.service';

@Controller()
export class MyController {
  constructor(private readonly [1]: MyService) {}

  getHello(): string {
    return this.myService.getHello();
  }
}
Drag options to blanks, or click blank then click option'
Aservice
BserviceInstance
CMyService
DmyService
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'MyService' as the variable name.
Using unrelated variable names.
3fill in blank
hard

Fix the error in the service decorator import statement.

NestJS
import { Injectable } from '@nestjs/[1]';

@Injectable()
export class UserService {}
Drag options to blanks, or click blank then click option'
Acommon
Bservices
Ccore
Dmodules
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Injectable from '@nestjs/core' or other incorrect paths.
Misspelling the package name.
4fill in blank
hard

Fill both blanks to create a provider and inject it into a module.

NestJS
import { Module, [1] } from '@nestjs/common';
import { AppService } from './app.service';

@Module({
  providers: [AppService],
  exports: [[2]],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
AInjectable
BAppService
CController
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using Controller or Service instead of Injectable in imports.
Exporting the wrong class or forgetting to export.
5fill in blank
hard

Fill all three blanks to define an injectable service with a method and inject it into a controller.

NestJS
import { [1] } from '@nestjs/common';

@[1]()
export class GreetingService {
  sayHello(): string {
    return 'Hello!';
  }
}

import { Controller } from '@nestjs/common';
import { GreetingService } from './greeting.service';

@Controller()
export class GreetingController {
  constructor(private readonly {{BLANK_3}}: GreetingService) {}

  greet(): string {
    return this.{{BLANK_3}}.sayHello();
  }
}
Drag options to blanks, or click blank then click option'
AInjectable
BgreetingService
CGreetingService
Dservice
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'GreetingService' as the injected variable name.
Forgetting to import Injectable.