Hello, I set up a hook on nestJS using MongooseModule, but I'm having trouble injecting third-party services

huangapple go评论59阅读模式
英文:

Hello, I set up a hook on nestJS using MongooseModule, but I'm having trouble injecting third-party services

问题

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { Commande, CommandeSchema } from 'src/commande/commande.model';

@Module({
    imports: [
        MongooseModule.forFeatureAsync([
            {
                name: 'Commande',
                imports: [SharedModule],
                inject: [MyService],
                useFactory: async (myService: MyService) => {
                    const schema = CommandeSchema;
                    schema.post('save', async function (doc) {
                        const commande = doc as unknown as Commande;
                        await myService.savingLivraison(commande);
                    });
                    return schema;
                },
            },
        ]),
    ],
    providers: [MyService],
    exports: [MyService]
})
export class SharedModule { }
英文:
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { Commande, CommandeSchema } from 'src/commande/commande.model';

@Module({
    imports: [
        MongooseModule.forFeatureAsync([
            {
                name: 'Commande',
                imports: [SharedModule],
                inject: [MyService],
                useFactory: async (myService: MyService) => {
                    const schema = CommandeSchema;
                    schema.post('save', async function (doc) {
                        const commande = doc as unknown as Commande;
                        await myService.savingLivraison(commande);
                    });
                    return schema;
                },
            },
        ]),],
    providers: [MyService],
    exports: [MyService]
})
export class SharedModule { }

when I do that the SharedModule module does not load while I load it well in the AppModule and when I remove the exports line in the shared module I have the error: `[Nest] 25850 - 08/06/2023 17:02:52 ERROR [ExceptionHandler] Nest can't resolve dependencies of the CommandeModel (DatabaseConnection, ?, MyService).

what I want is to be able to use the myService in the context of MongooseModule

答案1

得分: 0

你已经在SharedModule中创建了循环依赖。SharedModule需要导入MongooseModule,但MongooseModule也需要导入SharedModule。您需要将MyService移到SharedModule之外的一个模块中,然后可以通过SharedModuleMongooseModule导入该新模块,然后SharedModule可以重新导出此新模块,以使MyService在导入SharedModule的任何地方都可用。

英文:

You've created a circular dependency of the SharedModule on itself. The SharedModule needs to import the MongooseModule, but the MongooseModule needs to import the SharedModule. What you need to do is move the MyService to a module outside of the SharedModule which can then be imported by the SharedModule and the MongooseModule, and then the SharedModule can re-export this new module to make the MyService available anywhere theSharedModule is imported

huangapple
  • 本文由 发表于 2023年6月9日 01:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定