假设我的模块定义如下:
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
// Use ConfigService here
secretOrPrivateKey: 'secretKey',
signOptions: {
expiresIn: 3600,
},
}),
PrismaModule,
],
providers: [AuthResolver, AuthService, JwtStrategy],
})
export class AuthModule {}
现在如何从这里的ConfigService
获取secretKey
?
最佳答案
您必须使用 registerAsync
,这样您就可以注入(inject)您的 ConfigService
。使用它,您可以导入模块、注入(inject)提供程序,然后在返回配置对象的工厂函数中使用这些提供程序:
JwtModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
secretOrPrivateKey: configService.getString('SECRET_KEY'),
signOptions: {
expiresIn: 3600,
},
}),
inject: [ConfigService],
}),
有关详细信息,请参阅 async options docs .
关于javascript - 如何从 NestJS 的模块导入中获取配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54308318/