Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a basic NestJS service class.
NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class [1] { getHello(): string { return 'Hello World!'; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'AppController' instead of a service name.
Forgetting to add the @Injectable() decorator.
✗ Incorrect
The service class is usually named with 'Service' suffix, and 'AppService' is the common default name.
2fill in blank
mediumComplete the code to inject the service into a controller constructor.
NestJS
import { Controller } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() export class AppController { constructor(private readonly [1]: AppService) {} }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'AppService' as the property name.
Using unrelated names like 'service' or 'appcontroller'.
✗ Incorrect
The injected service is usually named in camelCase matching the class name, here 'appService'.
3fill in blank
hardFix the error in the service method to return a string.
NestJS
export class AppService { getHello(): [1] { return '123'; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a number when the return type is string.
Using 'void' as return type but returning a value.
✗ Incorrect
The method should return a string, so the return type must be 'string' and the return value a string.
4fill in blank
hardFill both blanks to create a service method that returns a greeting with a name parameter.
NestJS
export class AppService { getGreeting([1]: string): string { return `Hello, [2]!`; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and the template variable.
Forgetting to add the parameter type.
✗ Incorrect
The parameter and the template variable must match, here both are 'name'.
5fill in blank
hardFill all three blanks to create a service that injects a repository and uses it in a method.
NestJS
import { Injectable } from '@nestjs/common'; import { [1] } from './user.repository'; @Injectable() export class UserService { constructor(private readonly [2]: [1]) {} findAll() { return this.[2].findAllUsers(); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing service and repository names.
Using inconsistent property names.
✗ Incorrect
The repository class is 'UserRepository', injected as 'userRepository', which is used in the method.