解决NestJs动态模块中的循环依赖

时间:2020-06-19 06:26:56

标签: typescript nestjs

我刚刚开始将NestJs用于REST API项目。 我有两个相互依赖的模块,从而导致循环依赖问题。我可以通过使用forwardRef()机制解决这些问题。这适用于静态模块,但是,当我在动态模块中使用它时,出现以下错误。

[Nest] 18040   - 06/19/2020, 10:32:46 AM   [NestFactory] Starting Nest application...
[Nest] 18040   - 06/19/2020, 10:32:46 AM   [ExceptionHandler] metatype is not a constructor +65ms
TypeError: metatype is not a constructor
    at Injector.instantiateClass (E:\works\server\node_modules\@nestjs\core\injector\injector.js:289:19)
    at callback (E:\works\server\node_modules\@nestjs\core\injector\injector.js:76:41)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Injector.resolveConstructorParams (E:\works\server\node_modules\@nestjs\core\injector\injector.js:117:24)
    at async Injector.loadInstance (E:\works\server\node_modules\@nestjs\core\injector\injector.js:80:9)
    at async Injector.loadProvider (E:\works\server\node_modules\@nestjs\core\injector\injector.js:37:9)
    at async Promise.all (index 0)
    at async InstanceLoader.createInstancesOfProviders (E:\works\server\node_modules\@nestjs\core\injector\instance-loader.js:42:9)
    at async E:\works\server\node_modules\@nestjs\core\injector\instance-loader.js:27:13
    at async Promise.all (index 17)
    at async InstanceLoader.createInstances (E:\works\server\node_modules\@nestjs\core\injector\instance-loader.js:26:9)
    at async InstanceLoader.createInstancesOfDependencies (E:\works\server\node_modules\@nestjs\core\injector\instance-loader.js:16:9)
    at async E:\works\server\node_modules\@nestjs\core\nest-factory.js:81:17
    at async Function.asyncRun (E:\works\server\node_modules\@nestjs\core\errors\exceptions-zone.js:17:13)
    at async NestFactoryStatic.initialize (E:\works\server\node_modules\@nestjs\core\nest-factory.js:79:13)
    at async NestFactoryStatic.create (E:\works\server\node_modules\@nestjs\core\nest-factory.js:30:9)

请让我知道我要去哪里错了?

以下是导致此问题的两个模块:

AdvertisersModule:

@Module({
  imports: [
    MongooseModule.forFeatureAsync([
      {
        name: 'Advertiser',
        imports: [ forwardRef(() => AdvertisementsModule) ] // line that's causing error
        useFactory: (advertisementModel: Model<Advertisement>) => {
          const advertiserSchema = AdvertiserSchema;
          advertiserSchema.post('remove', async function(){
            await advertisementModel.deleteMany({ advertiser: this._id });
          });
          return advertiserSchema;
        },
        inject: [getModelToken('Advertisement')]
      }
    ]),
    forwardRef(() => AdvertisementsModule)
  ],
  exports: [ MongooseModule ],
  controllers: [AdvertisersController],
  providers: [AdvertisersService]
})
export class AdvertisersModule {}

AdvertisementsModule:

@Module({
  imports: [
    MongooseModule.forFeatureAsync([
      { 
        name: 'Advertisement', 
        imports: [ forwardRef(() => AdvertisersModule) ], // line that's causing error
        useFactory: (advertiserModel: Model<Advertiser>) => { 
        const advertisementSchema = AdvertisementSchema;

        advertisementSchema.post('save', async function() {
          let advertiser = await advertiserModel.findById(this.advertiser);
          if(advertiser) {
            advertiser.advertisements.push(this._id);
            advertiser.save();
          }
        });
        return advertisementSchema;
      },
      inject: [
        getModelToken('Advertiser')
      ]
    }
    ]),
    forwardRef(() => AdvertisersModule),
    forwardRef(() => SharedModule) 
  ],
  exports: [
    MongooseModule
  ],
  controllers: [AdvertisementsController],
  providers: [AdvertisementsService]
})
export class AdvertisementsModule {}

谢谢。

0 个答案:

没有答案