【问题标题】:Import a module which has a dynamic module as dependency in nest.js在 nest.js 中导入具有动态模块作为依赖项的模块
【发布时间】:2021-09-26 09:14:21
【问题描述】:

我是nest.js 的新手,我正在尝试在 APP 模块中使用 Q 模块中的服务。 Q 模块正在为队列导入动态BullModule ...

q.module.ts

import { BullModule } from '@nestjs/bull';
import { Module } from '@nestjs/common';
import { join } from 'path';
import { QService } from './q.service';

@Module({
  imports: [
    BullModule.registerQueue({
      name: 'volume',
      processors: [
        {
          name: 'create',
          path: join(__dirname, 'vol.processor.js'),
        },
      ],
    }),
  ],
  providers: [QService],
})

app.module.ts

const ENV = process.env.NODE_ENV;
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: !!ENV ? `.env.${ENV}` : '.env',
    }),
    BullModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        redis: {
          host: configService.get('REDIS_HOST'),
          port: Number(configService.get('REDIS_PORT')),
          password: configService.get('REDIS_PASSWORD'),
        },
      }),
      inject: [ConfigService],
    }),
    IdentityModule,
    QModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    IdentityService,
    QService,
  ],
})
export class AppModule {}

巢说:

Nest can't resolve dependencies of the QService (?). Please make sure that the argument BullQueue_volume at index [0] is available in the AppModule context.

Potential solutions:
- If BullQueue_volume is a provider, is it part of the current AppModule?
- If BullQueue_volume is exported from a separate @Module, is that module imported within AppModule?
  @Module({
    imports: [ /* the Module containing BullQueue_volume */ ]
  })

我该如何处理? 我读了一些文档,但我找不到:(

【问题讨论】:

  • 告诉我们QServiceconstructor

标签: nestjs nest-dynamic-modules


【解决方案1】:

而不是在AppModuleproviders 中重新定义QServiceQModule 应该在两个 providersexports 中都有QService。这将允许imports: [QModule] 的任何模块使用QModule 的导出,而无需重新定义提供程序。

附带说明,每次您将提供程序添加到模块的providers 数组时,这将是该提供程序的一个新实例。在这种情况下,有两个实例,AppModule 想要访问 QService 需要的所有内容。这就是为什么你应该改用exports

【讨论】:

    猜你喜欢
    • 2013-08-15
    • 2023-03-29
    • 2019-02-25
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多