[英]NestJs Using Environment Configuration on @Cron decorator
我正在構建一個帶有調度和配置的 nestJs 應用程序。 我希望能夠使用我的環境變量配置我的 Cron,但它似乎不起作用。
app.module.ts :
@Module({
imports: [
ConfigModule.forRoot(),
ScheduleModule.forRoot(),
SchedulingModule,
...
],
})
export class AppModule {}
schedule.service.ts(來自我的 SchedulingModule):
@Cron(process.env.CRON_VALUE)
scheduledJob() {
this.logger.log('Scheduled : Job');
...
}
.env :
...
CRON_VALUE=0 4 * * *
...
顯然,目前檢查該值是空的。 我收到以下錯誤:
(node:55016) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_isAMomentObject' of undefined
at new CronTime (/Users/antoinegrenard/Documents/Projet/b4finance/service-scheduling/node_modules/cron/lib/cron.js:42:50)
at new CronJob (/Users/antoinegrenard/Documents/Projet/b4finance/service-scheduling/node_modules/cron/lib/cron.js:527:19)
at /Users/antoinegrenard/Documents/Projet/b4finance/service-scheduling/node_modules/@nestjs/schedule/dist/scheduler.orchestrator.js:56:29
...
要解決此問題,您應該再次在您的服務上加載配置:
require('dotenv').config();
顯然,不可能在裝飾器中獲得 env 值。
我不得不這樣做:
constructor(private schedulerRegistry: SchedulerRegistry) {}
onModuleInit() {
const job = new CronJob(process.env. CRON_VALUE, () => {
// What you want to do here
});
this.schedulerRegistry.addCronJob(name, job);
job.start();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.