所以,這是我的 NEST JS 基本應(yīng)用程序。./shared/utils/config/index.tsexport default () => ({ PORT: parseInt(process.env.PORT, 10) || 3000, TO_PRINT_RESPONSE: JSON.parse(process.env.TO_PRINT_RESPONSE),});應(yīng)用程序模塊.tsimport CONFIG from './shared/utils/config/';@Module({ imports: [ ConfigModule.forRoot({ isGlobal: true, load: [ CONFIG ], }) ] // some more Module Decorator Config})export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { consumer .apply(AuthMiddleware) .forRoutes({ path: '/someurl', method: RequestMethod.ALL }); // some more configuration code. }}主.ts// AppModule is app.module.ts imported variableimport { ConfigService } from '@nestjs/config';async function bootstrap() { const app: INestApplication = await NestFactory.create(AppModule, { logger: console, }); const configService = app.get(ConfigService); console.log(typeof configService.get<Boolean>('TO_PRINT_RESPONSE')); /* this is coming as String even when: * 1. I place <Boolean> as a type (I know its of no use, since it does not change the datatype) * 2. But in config/index.ts I parsed it in BOOLEAN using JSON.parse() */ }bootstrap();.env PORT=5000 TO_PRINT_RESPONSE=true現(xiàn)在:.env 正在通過dotenv模塊加載(https://docs.nestjs.com/techniques/configuration)在./shared/utils/config/index.ts中進(jìn)行調(diào)試,它正在受到攻擊。所以,有人可以告訴我,當(dāng)我以正確的格式( ./shared/utils/config/index.ts )加載 JSON 時(shí),我在讀取正確數(shù)據(jù)類型的 ENV 值時(shí)哪里做錯(cuò)了。謝謝&快樂編碼:)
ConfigService 即使在解析后也返回字符串值
翻過高山走不出你
2023-04-20 10:02:38