0
0
NestJSframework~10 mins

Provider scope (default, request, transient) 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 set the provider scope to default (singleton).

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

@Injectable({ scope: [1] })
export class MyService {}
Drag options to blanks, or click blank then click option'
AScope.DEFAULT
BScope.REQUEST
CScope.TRANSIENT
DScope.SINGLETON
Attempts:
3 left
💡 Hint
Common Mistakes
Using Scope.REQUEST or Scope.TRANSIENT when singleton is needed.
Using a non-existent Scope.SINGLETON value.
2fill in blank
medium

Complete the code to set the provider scope to request-scoped.

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

@Injectable({ scope: [1] })
export class RequestService {}
Drag options to blanks, or click blank then click option'
AScope.REQUEST
BScope.TRANSIENT
CScope.DEFAULT
DScope.SINGLETON
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing request scope with transient or default scopes.
Using Scope.SINGLETON which does not exist.
3fill in blank
hard

Fix the error in the provider scope declaration to make it transient.

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

@Injectable({ scope: [1] })
export class TransientService {}
Drag options to blanks, or click blank then click option'
AScope.DEFAULT
BScope.REQUEST
CScope.TRANSIENT
DScope.SINGLETON
Attempts:
3 left
💡 Hint
Common Mistakes
Using Scope.REQUEST instead of Scope.TRANSIENT.
Using Scope.DEFAULT which is singleton.
4fill in blank
hard

Fill both blanks to create a request-scoped provider and inject it into a controller.

NestJS
import { Injectable, Scope, Controller, Get } from '@nestjs/common';

@Injectable({ scope: [1] })
export class UserService {
  getUser() {
    return 'User Data';
  }
}

@Controller('users')
export class UserController {
  constructor(private readonly userService: [2]) {}

  @Get()
  getUser() {
    return this.userService.getUser();
  }
}
Drag options to blanks, or click blank then click option'
AScope.REQUEST
BUserService
CScope.TRANSIENT
DUserController
Attempts:
3 left
💡 Hint
Common Mistakes
Using the controller class as the injected type.
Setting the wrong scope for the provider.
5fill in blank
hard

Fill all three blanks to create a transient provider, inject it, and call its method in a controller.

NestJS
import { Injectable, Scope, Controller, Get } from '@nestjs/common';

@Injectable({ scope: [1] })
export class LoggerService {
  log(message: string) {
    return `Log: ${message}`;
  }
}

@Controller('logs')
export class LogController {
  constructor(private readonly logger: [2]) {}

  @Get()
  getLog() {
    return this.logger.[3]('Hello');
  }
}
Drag options to blanks, or click blank then click option'
AScope.TRANSIENT
BLoggerService
Clog
DScope.REQUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using request scope instead of transient.
Calling a non-existent method on the provider.