网站搜索

typescript - NestJS Nest 无法解析依赖错误


我正在使用 NestJS Graphql TypeOrm,我尝试运行程序但出现了这些错误。我没有使用任何字符串模块

如果 String 是从一个单独的 @Module 导出的,那么该模块是否已在 SellModule 中导入?我找不到 String 被导出

Nest 无法解析 SellService 的依赖项 (SellRepository, ?)。请确保索引 [1] 处的参数 String 在 SellModule 上下文中可用。

潜在的解决方案:

  • 如果 String 是一个提供者,它是当前 SellModule 的一部分吗?

  • 如果 String 是从一个单独的 @Module 导出的,该模块是否在 SellModule 中被导入?

    @Module({ imports: [ /* 包含 String 的模块 */ ] })

sell.module.ts

import { Module } from '@nestjs/common'
import { SellService } from './sell.service'
import { SellResolver } from './sell.resolver'
import { TypeOrmModule } from '@nestjs/typeorm'
import { Sell } from './entities/sell.entity'

@Module({
  providers: [SellResolver, SellService],
  imports: [TypeOrmModule.forFeature([Sell])]
})
export class SellModule {}

sell.service.ts

import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import {
  createErrorMessage,
  deleteErrorMessage,
  editErrorMessage,
  existErrorMessage,
  notFoundMessage,
  readErrorMessage,
  userNotMatchDeleteError,
  userNotMatchEditError
} from 'src/common/common.constants'
import { User } from 'src/users/entities/user.entity'
import { Repository } from 'typeorm'
import { AllSellOutput } from './dto/all-sell.dto'
import { CreateSellInput, CreateSellOutput } from './dto/create-sell.dto'
import { DeleteSellInput, DeleteSellOutput } from './dto/delete-sell.dto'
import { EditSellInput, EditSellOutput } from './dto/edit-sell.dto'
import { Sell } from './entities/sell.entity'

@Injectable()
export class SellService {
  constructor (
    @InjectRepository(Sell) private readonly sells: Repository<Sell>,
    private readonly message: string
  ) {
    this.message = 'Зарын'
  }

  async createSell (
    createSellInput: CreateSellInput
  ): Promise<CreateSellOutput> {
    try {
      const sell = await this.sells.findOne({
        where: { title: createSellInput.title }
      })

      if (sell) {
        return {
          ok: false,
          error: existErrorMessage(this.message)
        }
      }

      await this.sells.save(await this.sells.create(createSellInput))

      return {
        ok: true
      }
    } catch {
      return {
        ok: false,
        error: createErrorMessage(this.message)
      }
    }
  }

  async allSell (): Promise<AllSellOutput> {
    try {
      const sells = await this.sells.find()

      if (!sells) {
        return {
          ok: false,
          error: notFoundMessage(this.message)
        }
      }

      return {
        ok: true,
        result: sells
      }
    } catch {
      return {
        ok: false,
        error: readErrorMessage(this.message)
      }
    }
  }


}

我该怎么办?

答案#1

NestJS 尝试根据提供者中定义的键自行解析依赖项。您当前将 JavaScript 类与 Nest 依赖注入混合使用。

为了解决您的问题,像这样从构造函数中移除 message:

export class SellService {

private readonly message = 'Зарын'

  constructor (
    @InjectRepository(Sell) private readonly sells: Repository<Sell>) {}
...
}

你的消息是一个常量,所以你可以简单地将其定义为构造函数之外的私有只读消息。