英文:
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
之外的一个模块中,然后可以通过SharedModule
和MongooseModule
导入该新模块,然后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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论