Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller or @Module instead of @Injectable.
Forgetting to import the decorator.
✗ Incorrect
The @Injectable() decorator marks a class as a provider that can be injected into other classes.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'MyService' as the variable name.
Using unrelated variable names.
✗ Incorrect
The injected service is usually named with camelCase, matching the class name starting with lowercase.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Injectable from '@nestjs/core' or other incorrect paths.
Misspelling the package name.
✗ Incorrect
The Injectable decorator is imported from '@nestjs/common', not from other sub-packages.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Controller or Service instead of Injectable in imports.
Exporting the wrong class or forgetting to export.
✗ Incorrect
The Injectable decorator is imported to mark providers, and the AppService is exported from the module.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'GreetingService' as the injected variable name.
Forgetting to import Injectable.
✗ Incorrect
The Injectable decorator is used twice: once for import and once for decorating the class. The injected property name is camelCase 'greetingService'.