Search code examples
node.jsnestjsnode-modulestypeorm

Nest can't resolve dependency "Repository"


i have this error on nest.js, this is error:

Error nest.js:

[Nest] 70813  - 11/03/2023, 4:06:53 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the ChefService (ChefRepository, ?). Please make sure that the argument Repository at index [1] is available in the ChefModule context.

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

module.ts

import { Module } from '@nestjs/common';
import { ChefService } from './chef.service';
import { ChefController } from './chef.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Chef } from './entities/chef.entity';
import { UsersEntity } from 'src/domains/users/entities/users.entity';
import { UsersService } from 'src/domains/users/users.service';

@Module({
  imports: [
    TypeOrmModule.forFeature([Chef]),
    TypeOrmModule.forFeature([UsersEntity]),
  ],
  controllers: [ChefController],
  providers: [UsersService, ChefService],
  exports: [ChefService],
})
export class ChefModule {}

service.ts

@Injectable()
export class ChefService {
  constructor(
    @InjectRepository(Chef)
    @InjectRepository(UsersEntity)
    private repo: Repository<Chef>,
    private repoUser: Repository<UsersEntity>,
  ) {}

  async findAll(): Promise<Chef[]> {
    var chef = await this.repo.find();
    var users = await this.repoUser.find();
    let values = [];
    for (var i = 0; i < chef.length; i++) {
      var userSelect = users.find((user) => user.id == chef[i].profile.id);
      const temp = [
        userSelect.name,
        userSelect.type,
        userSelect.username,
        chef[i].image,
        chef[i].latitude,
        chef[i].longitude,
        chef[i].city,
        chef[i].nation,
        chef[i].id,
      ];
      values.push(temp);
    }

    return values;
  }

Solution

  • The decorators in the constructor of your ChefService are incorrect. One decorator per parameter.

    @Injectable()
    export class ChefService {
      constructor(
        @InjectRepository(Chef)
        private repo: Repository<Chef>,
        @InjectRepository(UsersEntity)
        private repoUser: Repository<UsersEntity>,
      ) {}
       ...
    }