0

Why do I keep getting this error saying

Error: Nest can't resolve dependencies of the CashInService (?). Please make sure that the argument CashInRepository at index [0] is available in the AppModule context.

This is the structure of my CashInService:

import { Injectable } from "@nestjs/common";
import { CashIn } from "../../models/cash-in.entity";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";

@Injectable()
export class CashInService {
  constructor(
    @InjectRepository(CashIn) private cashInRepository: Repository<CashIn>) {
  }

  add(entry: CashIn): Promise<CashIn> {
    return this.cashInRepository.save(entry)
  }

  get(id: string): Promise<CashIn | undefined> {
    return this.cashInRepository.findOne(id)
  }
  getAll(): Promise<CashIn[]> {
    return this.cashInRepository.find()
  }
}

And here is my CashIn entity:

import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class CashIn {
  @PrimaryGeneratedColumn()
  id?: string;
  @Column()
  cashAmount?: number;
// more columns...

And more importantly, here is my app module.

import { Module } from '@nestjs/common';
import { AppController } from './controllers/app/app.controller';
import { AppService } from './app.service';
import { TransactionController } from "./controllers/transaction/transaction.controller";
import { CashInService } from "./services/cash-in/cash-in.service";
import { MysqlConnectionOptions } from "typeorm/driver/mysql/MysqlConnectionOptions";
import { CashOutService } from "./services/cash-out/cash-out.service";
import { TypeOrmModule } from "@nestjs/typeorm";

const ORMConfig : MysqlConnectionOptions = {
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '',
  database: 'test',
  entities: ["dist/**/*.entity{.ts,.js}"],
  synchronize: true,
}

@Module({
  imports: [TypeOrmModule.forRoot(ORMConfig)],
  controllers: [AppController,TransactionController],
  providers: [AppService,CashInService,CashOutService],
})
export class AppModule {}
birukhimself
  • 193
  • 3
  • 14

1 Answers1

2

There's no TypeOrmModule.forFeature([CashIn]) in your AppModule where you declare the CashInServce to be used. This is important because it tells Nest to create a provider to be used from the TypeOrmModule.

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • Thank you! Sorry for the dumb question. I had the assumption that the forfeature was only needed when you are using multiple modules. Kudos! – birukhimself Jun 13 '21 at 23:31