You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NestJS无法解析SellService依赖:String参数注入问题求助

问题解决:Nest无法解析SellService的String类型依赖

错误原因

你的SellService构造函数中声明了string类型参数message,Nest依赖注入系统会尝试自动注入该参数,但原始类型(如string、number)无法被Nest自动解析——它不知道该提供什么具体值,因此抛出依赖无法解析的错误。

解决方法

你在构造函数里直接给message赋值了固定字符串'Зарын',完全不需要通过依赖注入获取这个值,直接将其改为类的内部属性即可:

修改后的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 { 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 {
  // 直接定义类属性,无需依赖注入
  private readonly message = 'Зарын';

  constructor (
    @InjectRepository(Sell) private readonly sells: Repository<Sell>
    // 移除string类型的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)
      }
    }
  }
}

额外说明(动态配置场景)

如果后续需要动态配置这个字符串(比如从配置文件读取),可以使用Nest的自定义提供者:

  1. 在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,
    // 添加自定义提供者,标识字符串值
    {
      provide: 'SELL_MESSAGE',
      useValue: 'Зарын'
    }
  ],
  imports: [TypeOrmModule.forFeature([Sell])]
})
export class SellModule {}
  1. 在sell.service.ts中通过@Inject注入:
import { Injectable, Inject } from '@nestjs/common'
// ...其他导入

@Injectable()
export class SellService {
  constructor (
    @InjectRepository(Sell) private readonly sells: Repository<Sell>,
    @Inject('SELL_MESSAGE') private readonly message: string
  ) {}

  // ...其他方法
}

但就当前你的代码场景而言,直接定义类内部属性是最简单有效的解决方式。

内容的提问来源于stack exchange,提问作者Bilguuntamir Soronzontugs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 22:50:37