修复 "Nest can't resolve dependencies of the CashInService."

Fix for "Nest can't resolve dependencies of the CashInService."

为什么我一直收到此错误提示

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.

这是我的 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()
  }
}

这是我的 CashIn 实体:

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

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

更重要的是,这是我的应用程序模块。

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

在您声明要使用 CashInServceAppModule 中没有 TypeOrmModule.forFeature([CashIn])。这很重要,因为它告诉 Nest 创建要从 TypeOrmModule.

使用的提供程序