Nest 无法解析依赖项“存储库”

问题描述 投票:0回答:1

我在 Nest.js 上遇到此错误,这是错误:

nest.js 错误:

[Nest] 70813  - 11/03/2023, 4:06:53 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the ChefService (ChefRepository, ?). Please make sure that the argument Repository at index [1] is available in the ChefModule context.

Potential solutions:
- If Repository is a provider, is it part of the current ChefModule?
- If Repository is exported from a separate @Module, is that module imported within ChefModule?
  @Module({
    imports: [ /* the Module containing Repository */ ]
  })

模块.ts

import { Module } from '@nestjs/common';
import { ChefService } from './chef.service';
import { ChefController } from './chef.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Chef } from './entities/chef.entity';
import { UsersEntity } from 'src/domains/users/entities/users.entity';
import { UsersService } from 'src/domains/users/users.service';

@Module({
  imports: [
    TypeOrmModule.forFeature([Chef]),
    TypeOrmModule.forFeature([UsersEntity]),
  ],
  controllers: [ChefController],
  providers: [UsersService, ChefService],
  exports: [ChefService],
})
export class ChefModule {}

服务.ts

@Injectable()
export class ChefService {
  constructor(
    @InjectRepository(Chef)
    @InjectRepository(UsersEntity)
    private repo: Repository<Chef>,
    private repoUser: Repository<UsersEntity>,
  ) {}

  async findAll(): Promise<Chef[]> {
    var chef = await this.repo.find();
    var users = await this.repoUser.find();
    let values = [];
    for (var i = 0; i < chef.length; i++) {
      var userSelect = users.find((user) => user.id == chef[i].profile.id);
      const temp = [
        userSelect.name,
        userSelect.type,
        userSelect.username,
        chef[i].image,
        chef[i].latitude,
        chef[i].longitude,
        chef[i].city,
        chef[i].nation,
        chef[i].id,
      ];
      values.push(temp);
    }

    return values;
  }
node.js nestjs node-modules typeorm
1个回答
1
投票

您的

ChefService
构造函数中的装饰器不正确。每个参数一个装饰器。

@Injectable()
export class ChefService {
  constructor(
    @InjectRepository(Chef)
    private repo: Repository<Chef>,
    @InjectRepository(UsersEntity)
    private repoUser: Repository<UsersEntity>,
  ) {}
   ...
}
© www.soinside.com 2019 - 2024. All rights reserved.