0
0
NestJSframework~10 mins

Constructor injection 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'
AcatsService
BCatsService
Cservice
DCats
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name with uppercase as the property name
Forgetting to mark the property as private readonly
2fill in blank
medium

Complete the code to declare the service type in the constructor.

NestJS
constructor(private readonly catsService: [1]) {}
Drag options to blanks, or click blank then click option'
AcatsService
BCats
CService
DCatsService
Attempts:
3 left
💡 Hint
Common Mistakes
Using the instance name instead of the class name as type
Using a generic name like 'Service' instead of the specific service class
3fill in blank
hard

Fix the error in the constructor injection syntax.

NestJS
constructor([1] catsService: CatsService) {}
Drag options to blanks, or click blank then click option'
Areadonly
Bprivate readonly
Cpublic
Dprotected
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting access modifiers
Using only 'public' or only 'readonly' without 'private'
4fill in blank
hard

Fill both blanks to complete the service injection and usage.

NestJS
export class CatsController {
  constructor([1] catsService: CatsService) {}

  findAll() {
    return this.catsService.[2]();
  }
}
Drag options to blanks, or click blank then click option'
Aprivate readonly
BgetCats
CfetchCats
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'public' instead of 'private readonly' in constructor
Calling a non-existent method like 'fetchCats' if the service method is 'getCats'
5fill in blank
hard

Fill all three blanks to define and inject a service, then use it in a method.

NestJS
import { Injectable } from '@nestjs/common';

@Injectable()
export class DogsService {
  getDogs() {
    return ['Bulldog', 'Beagle'];
  }
}

export class DogsController {
  constructor([1] dogsService: [2]) {}

  listDogs() {
    return this.dogsService.[3]();
  }
}
Drag options to blanks, or click blank then click option'
Aprivate readonly
BDogsService
CgetDogs
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'public' instead of 'private readonly' for injection
Wrong service type or method name