我正在使用 NestJS Graphql TypeOrm 尝试运行程序,但出现了这些错误。我没有使用任何字符串模块
如果 String 是从单独的 @Module 导出的,那么该模块是否会在 SellModule 中导入? 我找不到字符串已导出
Nest 无法解析 SellService(SellRepository,?)的依赖关系。请确保索引 [1] 处的参数字符串在 SellModule 上下文中可用。
潜在的解决方案:
如果 String 是提供者,它是当前 SellModule 的一部分吗?
如果 String 是从单独的 @Module 导出的,那么该模块是否会在 SellModule 中导入?
@模块({ 导入:[/* 包含字符串的模块 */] })
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 {}
销售.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)
}
}
}
}
我应该做什么?
最佳答案
NestJS 尝试根据提供者中定义的键来解决自身的依赖关系。您目前将 JavaScript 类与 Nest 依赖注入(inject)混合在一起。
要解决您的问题,请从构造函数中删除消息
,例如:
export class SellService {
private readonly message = 'Зарын'
constructor (
@InjectRepository(Sell) private readonly sells: Repository<Sell>) {}
...
}
您的消息是一个常量,因此您可以简单地将其定义为构造函数外部的私有(private)只读消息。
关于typescript - NestJS Nest 无法解析依赖项错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73782726/