Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a custom provider token.
NestJS
const MY_SERVICE = '[1]';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic string like 'SERVICE' which may cause conflicts.
Using variable names instead of string tokens.
✗ Incorrect
The token MyServiceToken is a common pattern to identify a custom provider uniquely.
2fill in blank
mediumComplete the code to provide a custom class provider in NestJS.
NestJS
providers: [{ provide: MY_SERVICE, use[1]: MyServiceClass }] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'usevalue' or 'usevalue' instead of 'useClass'.
Confusing 'useFactory' with 'useClass'.
✗ Incorrect
The useClass key tells NestJS to use the given class as the provider implementation.
3fill in blank
hardFix the error in the custom provider definition by completing the blank.
NestJS
providers: [{ provide: MY_SERVICE, use[1]: () => new MyService() }] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'useClass' with a function instead of a class.
Using 'useValue' with a function instead of a value.
✗ Incorrect
When providing a factory function, the correct key is useFactory.
4fill in blank
hardFill both blanks to create a custom provider that uses a value.
NestJS
providers: [{ provide: [1], use[2]: { name: 'Test' } }] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'useClass' when providing a value object.
Using an undefined token name.
✗ Incorrect
Use MY_SERVICE as the token and useValue to provide a fixed value.
5fill in blank
hardFill all three blanks to create a factory provider with dependencies.
NestJS
providers: [{ provide: [1], use[2]: (dep) => new MyService(dep), inject: [[3]] }] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'useClass' instead of 'useFactory' for factory providers.
Not listing dependencies in the inject array.
Using wrong token names.
✗ Incorrect
The token is MY_SERVICE, the key for factory is useFactory, and the dependency injected is DependencyService.