Nest can't resolve dependencies of X, but it should

Issue

I got the following modules / Services:

Order Module:

import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BarModule } from 'src/bar/bar.module';
import { ProductModule } from 'src/product/product.module';
import { TapModule } from 'src/tap/tap.module';
import { Order } from './entities/order.entity';
import { OrderController } from './order.controller';
import { OrderService } from './order.service';

@Module({
  imports: [
    TypeOrmModule.forFeature([Order]),
    forwardRef(() => TapModule),
    ProductModule,
    BarModule,
    ConfigModule,
  ],
  controllers: [OrderController],
  providers: [OrderService],
  exports: [OrderService],
})
export class OrderModule {}

OrderService

export class OrderService {
  constructor(
    @InjectRepository(Order) private orderRepository: Repository<Order>,
    private readonly tapService: TapService,
  ) {}
}

TapModule

import { forwardRef, Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Tap } from './entities/tap.entity';
import { BarModule } from 'src/bar/bar.module';
import { ConfigModule } from '@nestjs/config';
import { TapController } from './tap.controller';
import { TapService } from './tap.service';
import { OrderModule } from 'src/order/order.module';

@Module({
  imports: [
    TypeOrmModule.forFeature([Tap]),
    BarModule,
    forwardRef(() => OrderModule),
    ConfigModule,
  ],
  controllers: [TapController],
  providers: [TapService],
  exports: [TypeOrmModule, TapService],
})
export class TapModule {}

TapService

@Injectable()
export class TapService {
  constructor(
    @InjectRepository(Tap) private tapRepository: Repository<Tap>,
    private readonly barService: BarService,
    private readonly customerService: CustomerService,
    private readonly orderService: OrderService,
    private readonly productService: ProductService,
  ) {}
}

Although I think I imported everything I should I get this error:

Nest can't resolve dependencies of the OrderService (OrderRepository, ?). Please make sure that the argument dependency at index [1] is available in the OrderModule context.

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

I did research a lot but didnt quite find anything. Or anything I found was exactly like that. I hope someone can help me and finds the error.
Thanks in advance!

Solution

Try to update your service:

export class OrderService {
 constructor(
    @InjectRepository(Order) private orderRepository: Repository<Order>,
    @Inject(forwardRef(() => TapService))
    private readonly tapService: TapService,
  ) {}
}

Answered By – Vladyslav Frizen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published