Contents

NestJS: Service Cannot Find Repository Dependency

Contents

NestJS: Service Cannot Find Repository Dependency

Cause

The following code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

@Injectable()
export class SiteRbacService {
  constructor(
    @InjectRepository(RbacEntity)
    public readonly repository: Repository<RbacEntity>,
  ) {}
  create(createRbacDto: CreateRbacDto) {
    return 'This action adds a new rbac';
  }

  findAll() {
    return `This action returns all rbac`;
  }

  findOne(id: number) {
    return `This action returns a #${id} rbac`;
  }

  update(id: number, updateRbacDto: UpdateRbacDto) {
    return `This action updates a #${id} rbac`;
  }

  remove(id: number) {
    return `This action removes a #${id} rbac`;
  }
}

At runtime it reports: Nest can't resolve dependencies of the SiteRbacService, and the log shows that @InjectRepository(RbacEntity) public readonly repository: Repository<RbacEntity> was not injected.

Solution

Add the following in site-rbac.module.ts:

1
2
3
4
5
6
7
@Module(
  {
    imports: [
      TypeOrmModule.forFeature([RbacEntity]),
    ]
  }
)