I'm trying to use an environment variable value in the nestjs/bull module's @Process() decorator, as follows. How should I provide the 'STAGE' variable as part of the job name?
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)
}
}
EDITED with answers (below) from Micael and Jay:
Micael Levi answered the initial question: You can't use the NestJS ConfigModule to get your config into a memory variable. However, running dotenv.config() in your bootstrap function will not work either; you get undefined values for the memory variables if you try to access them from within a Method Decorator. To resolve this, Jay McDoniel points out that you have to import the file before you import AppModule. So this works:
// 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();
You cannot use this
on that context due to how decorator evaluation works. At that time, there's no instance created for MqListener
class, thus, using this.configService
doens't make sense.
You'll need to access process.env.
directly. And so will call dotenv
(or what lib that read & parses your dot env file) in that file.