【问题标题】:Exception Handler in NestJsNestJs 中的异常处理程序
【发布时间】:2021-06-22 12:36:29
【问题描述】:

我是 NestJs 环境的新手,我目前面临这个错误,并在 github 上寻找问题解决方案,但无法帮助我得到错误的结果。

[Nest] 22140   - 21/06/2021 à 14:51:40   [ExceptionHandler] Nest can't resolve dependencies of the UsersRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.

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

我已经在我的 auth.module.ts 上声明了它

import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersRepository } from './users.repository';

@Module({
  imports: [TypeOrmModule.forFeature([UsersRepository])],
  providers: [AuthService],
  controllers: [AuthController],
})
export class AuthModule {}


并在我的服务文件中使用它。

import { Injectable } from '@nestjs/common';
import { UsersRepository } from './users.repository';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Injectable()
export class AuthService {
    constructor (
        @InjectRepository(UsersRepository)
        private usersRepository: UsersRepository,
    ) {}

    async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> {
        return this.usersRepository.createUser(authCredentialsDto);
    }
}

【问题讨论】:

  • 您的AppModule 中有TypeOrmModule.forRoot/Async(),对吗?你是如何运行产生错误的代码的?

标签: typescript nestjs


【解决方案1】:

只需使用您的 UsersRepository

例子


import { Injectable } from '@nestjs/common';
import { UsersRepository } from './users.repository';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthCredentialsDto } from './dto/auth-credentials.dto';

@Injectable()
export class AuthService {
    constructor (
        private usersRepository: UsersRepository,
    ) {}

    async signUp(authCredentialsDto: AuthCredentialsDto): Promise<void> {
        return this.usersRepository.createUser(authCredentialsDto);
    }
}

如果您有自定义存储库,则不需要 InjectRepository,因为它会为您传入的实体返回默认存储库

【讨论】:

  • 你好,即使使用此解决方案,错误仍然存​​在:[Nest] 24504 - 22/06/2021 à 06:28:55 [ExceptionHandler] Nest can't resolve dependencies of the UsersRepository (?). Please make sure that the argument Connection at index [0] is available in the TypeOrmModule context.
猜你喜欢
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 2020-10-20
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 2022-11-30
相关资源
最近更新 更多