Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nest can't resolve dependencies of repository

I got error on my nestjs app. I cant figure out whats wring with my code. I The codes is something like this

AppModule

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AdminModule } from './components/admin.modules';

@Module({
    imports: [
        AdminModule,
        TypeOrmModule.forRoot({
          type: 'postgres',
          host: process.env.DATABASE_HOST,
          username: process.env.DATABASE_USERNAME,
          password: process.env.DATABASE_PASSWORD,
          database: process.env.DATABASE_NAME,
          port: parseInt(process.env.DATABASE_PORT),
        }),
    ],
})
export class AppModule {}

AdminModule

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { Admin } from './admin.entity';
import { AdminRepository } from './admin.repository';

@Module({
    imports: [TypeOrmModule.forFeature([Admin])],
    providers: [AdminRepository],
})
export class AdminModule {}

AdminRepository

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { RepositoryBase } from '../../common/base/repository.base';
import { Admin } from './admin.entity';

@Injectable()
export class AdminRepository extends RepositoryBase<Admin> {
    constructor(@InjectRepository(Admin) private readonly repo: Repository<Admin>) {
        super(repo);
}

And what i get is error like this

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

Potential solutions: - If AdminRepository is a provider, is it part of the current AdminModule? - If AdminRepository is exported from a separate @Module, is that module imported within AdminModule?

like image 960
sundaem Avatar asked Feb 19 '20 23:02

sundaem


1 Answers

I think the problem is the name you are giving to the provider: AdminRepository. Quite likely, internally TypeOrm is using that name to create a repository for the Admin entity and hence the name clash.

If you rename your provider class to anything else, (e.g. AdminRepo or AdminService), the error should go away. Or another thing to do would be to rename the entity class from Admin to something else.

What you want to avoid is creating a class with ${EntityClassName}Repository

like image 52
An Vad Avatar answered Nov 10 '22 15:11

An Vad