0
0
NestJSframework~10 mins

Async configuration 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 import ConfigModule asynchronously.

NestJS
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [ConfigModule.[1]({
    isGlobal: true,
  })],
})
export class AppModule {}
Drag options to blanks, or click blank then click option'
Acreate
BforFeature
Cregister
DforRootAsync
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous methods like forFeature instead of forRootAsync.
Trying to use register which is not a method of ConfigModule.
2fill in blank
medium

Complete the code to provide a factory function for async config.

NestJS
ConfigModule.forRootAsync({
  useFactory: async () => {
    const config = await fetchConfig();
    return [1];
  },
}),
Drag options to blanks, or click blank then click option'
APromise.resolve(config)
Bconfig
Cawait config
D{ config }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning an object with config as a property instead of the config itself.
Using await unnecessarily on a variable already awaited.
3fill in blank
hard

Fix the error in the async config provider by completing the inject array.

NestJS
ConfigModule.forRootAsync({
  imports: [HttpModule],
  useFactory: async (httpService: HttpService) => {
    const response = await httpService.get('url').toPromise();
    return response.data;
  },
  inject: [[1]],
}),
Drag options to blanks, or click blank then click option'
AHttpModule
BHttpClient
CHttpService
DHttp
Attempts:
3 left
💡 Hint
Common Mistakes
Injecting the module instead of the service.
Using incorrect service names like HttpClient.
4fill in blank
hard

Fill both blanks to create an async config that uses a class provider.

NestJS
ConfigModule.forRootAsync({
  useClass: [1],
  imports: [[2]],
}),
Drag options to blanks, or click blank then click option'
AConfigService
BHttpModule
CConfigModule
DAppModule
Attempts:
3 left
💡 Hint
Common Mistakes
Using the module name instead of the service class for useClass.
Importing the wrong module or no module at all.
5fill in blank
hard

Fill all three blanks to create a complete async config with useFactory, inject, and imports.

NestJS
ConfigModule.forRootAsync({
  imports: [[1]],
  useFactory: async (configService: [2]) => {
    const settings = await configService.loadSettings();
    return settings;
  },
  inject: [[3]],
}),
Drag options to blanks, or click blank then click option'
AHttpModule
BConfigService
DAppModule
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up module and service names in imports and inject arrays.
Not importing the module that provides the injected service.