Home > Enterprise >  Nest can't resolve dependencies of the ResolutionSevice
Nest can't resolve dependencies of the ResolutionSevice

Time:10-08

im currently exploring nestjs and came across this error:

Error: Nest can't resolve dependencies of the ResolutionService (?). Please make sure that the argument ResolutionRepository at index [0] is available in the ResolutionService context.

Potential solutions:

  • If ResolutionRepository is a provider, is it part of the current ResolutionService?
  • If ResolutionRepository is exported from a separate @Module, is that module imported within ResolutionService? @Module({ imports: [ /* the Module containing ResolutionRepository */ ] })

What am I doing wrong here?

resolution.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ResolutionEntity } from './resolution.entity';
import { ResolutionService } from './resolution.service';
import { ResolutionRepository } from './resolution.repository';

@Module({
  imports: [TypeOrmModule.forFeature([ResolutionEntity])],
  providers: [ResolutionRepository, ResolutionService],
  exports: [ResolutionService],
})
export class ResolutionModule {}

resolution.service.ts

import { Injectable } from '@nestjs/common';
import { ResolutionEntity } from './resolution.entity';
import { ResolutionRepository } from './resolution.repository';

@Injectable()
export class ResolutionService {
  constructor(private readonly resolutionRepository: ResolutionRepository) {}

  async getAllByName(name: string): Promise<ResolutionEntity[]> {
    return this.resolutionRepository.getAllByName(name);
  }
}

resolution.repository.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ResolutionEntity } from './resolution.entity';

@Injectable()
export class ResolutionRepository {
  constructor(
    @InjectRepository(ResolutionEntity)
    private readonly repository: Repository<ResolutionEntity>,
  ) {}

  async getAllByName(name: string): Promise<ResolutionEntity[]> {
    const resolutions = await this.repository
      .createQueryBuilder('res')
      .leftJoinAndSelect('res.patient', 'p')
      .where('p.name = :name', { name })
      .getMany();
    return resolutions;
  }
}

CodePudding user response:

You have ResolutionService in an imports array somewhere in your application. Providers never go in the imports array, only in the providers. If you need this provider in another module, the ResolutionModule should have the ResolutionService in the providers and exports arrays, and then this new module should have ResolutionModule in the imports array.

CodePudding user response:

Instead of

TypeOrmModule.forFeature([ResolutionEntity])

Maybe you want

TypeOrmModule.forFeature([ResolutionEntity, ResolutionRepository])
  • Related