Nest 无法解析 BookingService 的依赖关系

时间:2021-03-15 14:55:54

标签: nestjs

我检查了很多答案,但仍然出现此错误。我希望你能帮我看看我错过了什么

<块引用>

错误:Nest 无法解析 BookingService 的依赖关系 (BookingRepository, ?, UserRepository)。请确保 索引 [1] 处的参数 TableRepository 在 BookingModule 上下文。可能的解决方案:

  • 如果 TableRepository 是一个提供者,它是否是当前 BookingModule 的一部分?
  • 如果 TableRepository 是从单独的 @Module 导出的,那么该模块是否导入到 BookingModule 中? @模块({ 导入:[ /* 包含 TableRepository 的模块 */ ] })

我的booking.service.ts

@Injectable()
export class BookingService {
    constructor(
        @InjectRepository(Booking)
        private readonly repository: Repository<Booking>,
        @InjectRepository(Table)
        private readonly tableRepository: Repository<Table>,
        @InjectRepository(User)
        private readonly userRepository: Repository<User>
    ) {}
  }

我的app.module.ts

@Module({
      imports: [
        TypeOrmModule.forRoot({ autoLoadEntities: true }),
        TableModule,
        RestaurantModule,
        UserModule,
        BookingModule
      ],
      controllers: [AppController],
      providers: [AppService],
      exports: [TypeOrmModule]
    })
    export class AppModule {}

我的booking.module.ts

@Module({
    imports: [TypeOrmModule.forFeature([Booking]), TableModule],
    controllers: [BookingController],
    providers: [BookingService]
})
export class BookingModule {}

我的table.module.ts

@Module({
    imports: [TypeOrmModule.forFeature([Table])],
    controllers: [TableController],
    providers: [TableService],
    exports: [TableService]
})
export class TableModule {}

1 个答案:

答案 0 :(得分:1)

如果您想注入 TableRepository,您应该将 TypeOrmModule.forFeature([Table]) 添加到 BookingModule,或者您应该将 TypeOrmModule 添加到 exports TableModule。后者只会创建 TableRepository 的一个实例,所以这是我的建议,但最终两者都会起作用。