NestJS Nest can't resolve dependencies Error

Im using NestJS Graphql TypeOrm im trying to run programm but got these error. I did not use any String Module

If String is exported from a separate @Module, is that module imported within SellModule? I can't find String is exported

Nest can't resolve dependencies of the SellService (SellRepository, ?). Please make sure that the argument String at index [1] is available in the SellModule context.

Potential solutions:

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)
      }
    }
  }


}

What should I do?

Upvotes: 2

Views: 917

Answers (1)

Robert-Jan Kuyper
Robert-Jan Kuyper

Reputation: 3306

NestJS tries to resolve the dependencies it self based on keys, defined in providers. You currently mix JavaScript classes with Nest dependency injection.

To solve your issue, remove the message from the constructor like:

export class SellService {

private readonly message = 'Зарын'

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

Your message is a constant, so you can simply define it as a private readonly message outside the constructor.

Upvotes: 2

Related Questions