提问人:Danyal Sandeelo 提问时间:11/16/2023 最后编辑:Danyal Sandeelo 更新时间:11/16/2023 访问量:18
依赖项在 NestJS 处理器中未定义 - @nestjs/bull
Dependencies are undefined in NestJS processors - @nestjs/bull
问:
我已经在我的公牛中配置了队列机制,我可以轻松访问生产者发送的消息进行处理。我将使用此信息进行一些后台处理。
问题是,我有一些需要访问的服务和存储库,但似乎我无法访问。 我在 NestJS 中编写了自定义使用者,我从应用程序中访问对象
await app.resolve<LoggerService>(LoggerService);
应用程序在哪里NestFastifyApplication
在 Bull 的情况下,构造函数注入不能直接在处理器中起作用。
export class PromotionProcessor {
public constructor(private readonly serviceLayer: ServiceLayer) { }
@Process(PACKAGE_PROMOTION)
async processMtPackagePromotion(job: Job<PackagePromotionRequest>):Promise<void> {
const { data } = job;
const messageData = data as unknown as PromotionRequest;
await this.serviceLayer.promoteToNextLifecycle(messageData.data, messageData.reqData);
}
}
如何在此处使用存储库和服务?
@Module({
imports: [...APP_IMPORTS],
controllers: [...APP_CONTROLLERS],
providers: [...APP_PROVIDERS],
})
APP_IMPORTS
具有 bull 配置,定义了所有存储库和服务APP_PROVIDERS
答:
1赞
Daniel Wasserlauf
11/16/2023
#1
这可能有帮助,也可能没有帮助。
要将服务注入到另一个类中,您需要在相应的文件中声明它,其中 是“提供者”.module.ts
PromotionProcessor
例如:
import { Module } from '@nestjs/common';
import { BullModule } from '@nestjs/bull';
import { ServiceLayerModule } from '../../serviceLayer/serviceLayer.module.js';
import { PromotionProcessor } from './PromotionProcessor.service.js';
@Module({
controllers: [],
providers: [PromotionProcessor],
imports: [
BullModule.registerQueue({
name: 'myQueueName',
}),
ServiceLayerModule,
],
})
export class PromotionProcessorModule {}
此外,还需要将 导入到PromotionProcessorModule
AppModule
然后,在包含该文件的文件中,您应该能够将文件注入到构造函数中。PromotionProcessor
有时可能会有一些循环引用,这些引用也会提醒您,以便您可以进行显式注入 如下所示:
import { Inject, forwardRef } from '@nestjs/common';
export class PromotionProcessor {
public constructor( @Inject(forwardRef(() => ServiceLayer)) private readonly serviceLayer: ServiceLayer) { }
@Process(PACKAGE_PROMOTION)
async processMtPackagePromotion(job: Job<PackagePromotionRequest>):Promise<void> {
const { data } = job;
const messageData = data as unknown as PromotionRequest;
await this.serviceLayer.promoteToNextLifecycle(messageData.data, messageData.reqData);
}
}
希望这在一定程度上有所帮助。
评论
0赞
Daniel Wasserlauf
11/16/2023
您可能不需要在模块的 import 语句中注册 BullModule,我只是将其保留以防万一。
0赞
Danyal Sandeelo
11/16/2023
谢谢,让我试试注入,我把公牛包含在同一个应用程序模块中,处理器和生产者在同一个供应商中。
1赞
Danyal Sandeelo
11/16/2023
更新了导入的详细信息。
评论