0

I'm having some trouble injecting the mongoose Connection object inside a global interceptor in nest js. Here is my main module

imports: [
    ConfigModule.forRoot({
        isGlobal: true,
        load: [getConfiguration]
    }),
    TypeOrmModule.forRootAsync({
        imports: [ConfigModule],
        useFactory: (config: ConfigService) => config.get<TypeOrmModuleOptions>('postgres'),
        inject: [ConfigService]
    }),
    MongooseModule.forRootAsync({
        imports: [ConfigModule],
        useFactory: (config: ConfigService) => {
            const options = config.get('mongo');
            const uri = `mongodb://${options.host}:${options.port}/${options.database}`;
            return { uri };
        },
        inject: [ConfigService]
    }),
    OrderModule
],
controllers: [],
providers: [
    {
        provide: APP_INTERCEPTOR,
        useClass: TransactionInterceptor
    }
]

And here is the interceptor

@Injectable()
export class TransactionInterceptor implements NestInterceptor {
    constructor(
        private readonly pgConnection: PgConnection,
        private readonly mongoConnection: MongoConnection
    ) {}
}

For some reasone Nest can't resolve only the mongoose dependecy. The typeorm connetion gets injected just fine. I also tried with an async provider like

@Injectable()
export class TransactionInterceptor implements NestInterceptor {
    constructor(
        private readonly pgConnection: PgConnection,
        private readonly mongoConnection: MongoConnection
    ) {}

What am I missing? Thanks

  • What about using the `@InjectConnection()` decorator from `@nestjs//mongoose` and `@nestjs/typeorm` to inject the connections? You can still type them how you want, this just tells Nest specifically what the injection token is – Jay McDoniel Feb 12 '22 at 17:02

0 Answers0