0
0
NestJSframework~10 mins

Service creation 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 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'
AAppController
BMainService
CAppService
DHelloService
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'AppController' instead of a service name.
Forgetting to add the @Injectable() decorator.
2fill in blank
medium

Complete 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'
Aservice
BappService
Cappcontroller
DAppService
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'AppService' as the property name.
Using unrelated names like 'service' or 'appcontroller'.
3fill in blank
hard

Fix 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'
Astring
Bnumber
Cboolean
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a number when the return type is string.
Using 'void' as return type but returning a value.
4fill in blank
hard

Fill 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'
Aname
Busername
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and the template variable.
Forgetting to add the parameter type.
5fill in blank
hard

Fill 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'
AUserRepository
BuserRepository
CUserService
DuserService
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing service and repository names.
Using inconsistent property names.