最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nest can't resolve dependencies of repository - Stack Overflow

programmeradmin4浏览0评论

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/mon';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AdminModule } from './ponents/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/mon';
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/mon';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { RepositoryBase } from '../../mon/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?

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/mon';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AdminModule } from './ponents/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/mon';
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/mon';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { RepositoryBase } from '../../mon/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?

Share Improve this question asked Feb 19, 2020 at 23:59 sundaemsundaem 2031 gold badge3 silver badges5 bronze badges 1
  • Hi! Can you provide a repository.base file? – Yaro Commented Aug 7, 2022 at 8:00
Add a ment  | 

1 Answer 1

Reset to default 15

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

发布评论

评论列表(0)

  1. 暂无评论