我试图在nestjs/bull模块的@Process()装饰器中使用一个环境变量值,如下所示。我应该如何提供“阶段”变量作为工作名称的一部分?
import { Process, Processor } from '@nestjs/bull';
import { Inject } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Job } from 'bull';
@Processor('main')
export class MqListener {
constructor(
@Inject(ConfigService) private configService: ConfigService<SuperRootConfig>,
) { }
// The reference to configService is not actually allowed here:
@Process(`testjobs:${this.configService.get('STAGE')}`)
handleTestMessage(job: Job) {
console.log("Message received: ", job.data)
}
}编辑自Micael和Jay的答复(见下文):
Micael回答了最初的问题:您不能使用NestJS ConfigModule将配置输入到内存变量中。但是,在引导函数中运行dotenv.config()也不能工作;如果尝试从方法解码器中访问内存变量,则会获得未定义的值。要解决这个问题,Jay McDoniel指出在导入AppModule之前必须导入文件。因此,这是可行的:
// main.ts
import { NestFactory } from '@nestjs/core';
require('dotenv').config()
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT || 4500);
}
bootstrap();发布于 2021-12-28 02:47:12
由于this是如何工作的,所以不能在该上下文中使用装饰师评价。当时,还没有为MqListener类创建实例,因此,使用this.configService没有意义。
您需要直接访问process.env.。因此将在该文件中调用dotenv (或读取和解析点env文件的库)。
https://stackoverflow.com/questions/70502156
复制相似问题