Nest 无法解决 MerchantsService 的依赖项。

huangapple go评论85阅读模式
英文:

Nest can't resolve dependencies of the MerchantsService

问题

我正在学习nestjs,非常喜欢它的模块化方法。我试图在一个服务中导入另一个服务,以避免代码重复。以下是错误消息:

Nest无法解析MerchantsService的依赖关系 (?)。
请确保在RewardsModule上下文中索引[0]的参数MerchantEntityRepository可用。

可能的解决方案:
- RewardsModule是否是有效的NestJS模块?
- 如果MerchantEntityRepository是提供者,是否它是当前RewardsModule的一部分?
- 如果MerchantEntityRepository来自单独的@Module导出,是否在RewardsModule中导入了该模块?
  @Module({
    imports: [ /* 包含MerchantEntityRepository的模块 */ ]
  })

以下是我的基本设置。请注意,除了服务导入之外,一切都正常工作。还请注意,我没有一个被询问的 "MerchantEntityRepository" 类。

merchants.controller.ts

import {
  Controller,
  Get,
  Param,
} from '@nestjs/common';
import { MerchantsService } from './merchants.service';

@Controller('merchants')
export class MerchantsController {
  constructor(private readonly merchantsService: MerchantsService) {}

  @Get(':id')
  async findOne(@Param('id') id: number) {
    return this.merchantsService.findOne(+id);
  }
}

merchants.services.ts

import {
  Injectable,
  NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { MerchantEntity } from './entities/merchant.entity';
import { Repository } from 'typeorm';
import { errorMessages } from 'src/utility/common/error-mssages';

@Injectable()
export class MerchantsService {
  constructor(
    @InjectRepository(MerchantEntity)
    private merchantRepository: Repository<MerchantEntity>,
  ) {}

  async findOne(id: number): Promise<MerchantEntity> {
    const merchant: MerchantEntity = await this.merchantRepository.findOne({
      id,
    });

    if (!merchant)
      throw new NotFoundException(errorMessages.MERCHANT_UNKNOWN_ID);

    return merchant;
  }
}

merchants.module.ts

import { Module } from '@nestjs/common';
import { MerchantsService } from './merchants.service';
import { MerchantsController } from './merchants.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MerchantEntity } from './entities/merchant.entity';

@Module({
  imports: [TypeOrmModule.forFeature([MerchantEntity])],
  controllers: [MerchantsController],
  providers: [MerchantsService],
})
export class MerchantsModule {}

rewards.controller.ts

import {
  Controller,
  Post,
  Body,
  Param,
  UseGuards,
} from '@nestjs/common';
import { RewardsService } from './rewards.service';
import { CreateRewardDto } from './dto/create-reward.dto';
import { AuthenticationGuard } from 'src/utility/guards/authentication.guard';
import { AuthorizeGuard } from 'src/utility/guards/authorization.guard';
import { Roles } from 'src/utility/common/user-rules.enum';
import { CurrentUser } from 'src/utility/decorators/current-user.decorator';
import { UserEntity } from 'src/users/entities/user.entity';

@Controller('rewards')
export class RewardsController {
  constructor(private readonly rewardsService: RewardsService) {}

  @UseGuards(AuthenticationGuard, AuthorizeGuard([Roles.MERCHANT]))
  @Post('merchantID/:id/create')
  create(
    @Body() createRewardDto: CreateRewardDto,
    @Param('id') merchantID: number,
    @CurrentUser() currentUser: UserEntity,
  ) {
    return this.rewardsService.create(createRewardDto, merchantID, currentUser);
  }
}

rewards.services.ts

import { Injectable } from '@nestjs/common';
import { CreateRewardDto } from './dto/create-reward.dto';
import { UserEntity } from 'src/users/entities/user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { RewardEntity } from './entities/reward.entity';
import { Repository } from 'typeorm';
import { MerchantsService } from 'src/merchants/merchants.service';

@Injectable()
export class RewardsService {
  constructor(
    @InjectRepository(RewardEntity)
    private rewardRepository: Repository<RewardEntity>,

    private readonly merchantsService: MerchantsService,
  ) {}

  create(
    createRewardDto: CreateRewardDto,
    merchantID: number,
    currentUser: UserEntity,
  ) {
    const id = merchantID;
    const merchant = this.merchantsService.findOne(id);

    console.log(merchant);
  }
}

rewards.module.ts

import { Module } from '@nestjs/common';
import { RewardsService } from './rewards.service';
import { RewardsController } from './rewards.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RewardEntity } from './entities/reward.entity';
import { MerchantsService } from 'src/merchants/merchants.service';
import { MerchantsModule } from 'src/merchants/merchants.module';

@Module({
  imports: [TypeOrmModule.forFeature([RewardEntity]), MerchantsModule],
  controllers: [RewardsController],
  providers: [RewardsService, MerchantsService],
})
export class RewardsModule {}

希望这有助于解决你的问题。如果还有其他疑问,请随时提出。

英文:

I am learning nestjs and really like its modular approach. I am trying to import a service into another service to avoid code duplication. Here is the error message:

Nest can&#39;t resolve dependencies of the MerchantsService (?). 
Please make sure that the argument MerchantEntityRepository 
at index [0] is available in the RewardsModule context.

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

Here is my basic setup. Note, everything works except for the service import. Please also note that I do not have a "MerchantEntityRepository" class that it is asking about.

merchants.controller.ts

import {
  Controller,
  Get,
  Param,

} from &#39;@nestjs/common&#39;;
import { MerchantsService } from &#39;./merchants.service&#39;;


@Controller(&#39;merchants&#39;)
export class MerchantsController {
  constructor(private readonly merchantsService: MerchantsService) {}

  @Get(&#39;:id&#39;)
  async findOne(@Param(&#39;id&#39;) id: number) {
    return this.merchantsService.findOne(+id);
  }

}

merchants.services.ts

import {
  Injectable,
  NotFoundException,
} from &#39;@nestjs/common&#39;;
import { InjectRepository } from &#39;@nestjs/typeorm&#39;;
import { MerchantEntity } from &#39;./entities/merchant.entity&#39;;
import { Repository } from &#39;typeorm&#39;;
import { errorMessages } from &#39;src/utility/common/error-mssages&#39;;


@Injectable()
export class MerchantsService {
  constructor(
    @InjectRepository(MerchantEntity)
    private merchantRepository: Repository&lt;MerchantEntity&gt;, 
  ) {}

  async findOne(id: number): Promise&lt;MerchantEntity&gt; {
    const merchant: MerchantEntity = await this.merchantRepository.findOneBy({
      id,
    });

    if (!merchant)
      throw new NotFoundException(errorMessages.MERCHANT_UNKNOWN_ID);

    return merchant;
  }


}

merchants.module.ts

import { Module } from &#39;@nestjs/common&#39;;
import { MerchantsService } from &#39;./merchants.service&#39;;
import { MerchantsController } from &#39;./merchants.controller&#39;;
import { TypeOrmModule } from &#39;@nestjs/typeorm&#39;;
import { MerchantEntity } from &#39;./entities/merchant.entity&#39;;

@Module({
  imports: [TypeOrmModule.forFeature([MerchantEntity])],
  controllers: [MerchantsController],
  providers: [MerchantsService],
})
export class MerchantsModule {}

rewards.controller.ts

import {
  Controller,
  Post,
  Body,
  Param,
  UseGuards,
} from &#39;@nestjs/common&#39;;
import { RewardsService } from &#39;./rewards.service&#39;;
import { CreateRewardDto } from &#39;./dto/create-reward.dto&#39;;
import { AuthenticationGuard } from &#39;src/utility/guards/authentication.guard&#39;;
import { AuthorizeGuard } from &#39;src/utility/guards/authorization.guard&#39;;
import { Roles } from &#39;src/utility/common/user-rules.enum&#39;;
import { CurrentUser } from &#39;src/utility/decorators/current-user.decorator&#39;;
import { UserEntity } from &#39;src/users/entities/user.entity&#39;;

@Controller(&#39;rewards&#39;)
export class RewardsController {
  constructor(private readonly rewardsService: RewardsService) {}

  @UseGuards(AuthenticationGuard, AuthorizeGuard([Roles.MERCHANT]))
  @Post(&#39;merchantID/:id/create&#39;)
  create(
    @Body() createRewardDto: CreateRewardDto,
    @Param(&#39;id&#39;) merchantID: number,
    @CurrentUser() currentUser: UserEntity,
  ) {
    return this.rewardsService.create(createRewardDto, merchantID, currentUser);
  }
}

rewards.services.ts

import { Injectable } from &#39;@nestjs/common&#39;;
import { CreateRewardDto } from &#39;./dto/create-reward.dto&#39;;
import { UserEntity } from &#39;src/users/entities/user.entity&#39;;
import { InjectRepository } from &#39;@nestjs/typeorm&#39;;
import { RewardEntity } from &#39;./entities/reward.entity&#39;;
import { Repository } from &#39;typeorm&#39;;
import { MerchantsService } from &#39;src/merchants/merchants.service&#39;;

@Injectable()
export class RewardsService {
  constructor(
    @InjectRepository(RewardEntity)
    private rewardRepository: Repository&lt;RewardEntity&gt;,

    private readonly merchantsService: MerchantsService,
  ) {}

  create(
    createRewardDto: CreateRewardDto,
    merchantID: number,
    currentUser: UserEntity,
  ) {
    const id = merchantID;
    const merchant = this.merchantsService.findOne(id);

    console.log(merchant);
  }

rewards.module.ts

import { Module } from &#39;@nestjs/common&#39;;
import { RewardsService } from &#39;./rewards.service&#39;;
import { RewardsController } from &#39;./rewards.controller&#39;;
import { TypeOrmModule } from &#39;@nestjs/typeorm&#39;;
import { RewardEntity } from &#39;./entities/reward.entity&#39;;
import { MerchantsService } from &#39;src/merchants/merchants.service&#39;;
import { MerchantsModule } from &#39;src/merchants/merchants.module&#39;;

@Module({
  imports: [TypeOrmModule.forFeature([RewardEntity]), MerchantsModule],
  controllers: [RewardsController],
  providers: [RewardsService, MerchantsService],
})
export class RewardsModule {}

答案1

得分: 1

在你的 merchants.module.ts 中,你需要添加 exports: [MerchantService],以便在你的代码中使用 MerchantService

根据 NestJs 的文档exports 是:

> 由该模块提供的一部分提供者,应该在导入该模块的其他模块中可用。

import { Module } from '@nestjs/common';
import { MerchantsService } from './merchants.service';
import { MerchantsController } from './merchants.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MerchantEntity } from './entities/merchant.entity';

@Module({
  imports: [TypeOrmModule.forFeature([MerchantEntity])],
  controllers: [MerchantsController],
  providers: [MerchantsService],
  exports: [MerchantsService],  // <-- 添加这行
})
export class MerchantsModule {}
英文:

In your merchants.module.ts you have to add exports: [MerchantService] in order to use MerchantService in your code.

From NestJs docs, exports is:

> the subset of providers that are provided by this module and should be available in other modules which import this module.

import { Module } from &#39;@nestjs/common&#39;;
import { MerchantsService } from &#39;./merchants.service&#39;;
import { MerchantsController } from &#39;./merchants.controller&#39;;
import { TypeOrmModule } from &#39;@nestjs/typeorm&#39;;
import { MerchantEntity } from &#39;./entities/merchant.entity&#39;;

@Module({
  imports: [TypeOrmModule.forFeature([MerchantEntity])],
  controllers: [MerchantsController],
  providers: [MerchantsService],
  exports: [MerchantsService],  // &lt;-- add this line
})
export class MerchantsModule {}

答案2

得分: 0

在检查了一个中间帖子后,我发现奖励模块的正确配置如下所示:

import { Module } from '@nestjs/common';
import { RewardsService } from './rewards.service';
import { RewardsController } from './rewards.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { RewardEntity } from './entities/reward.entity';
import { MerchantsModule } from 'src/merchants/merchants.module';
@Module({
  imports: [TypeOrmModule.forFeature([RewardEntity]), MerchantsModule],
  controllers: [RewardsController],
  providers: [RewardsService],
})
export class RewardsModule {}

之后,我从merchants.module.ts中导出了:

exports: [MerchantsService],

如@Wuagliono所指出的。

英文:

After examining a median post, I found the proper configuration to be this for the rewards.module.ts

import { Module } from &#39;@nestjs/common&#39;;
import { RewardsService } from &#39;./rewards.service&#39;;
import { RewardsController } from &#39;./rewards.controller&#39;;
import { TypeOrmModule } from &#39;@nestjs/typeorm&#39;;
import { RewardEntity } from &#39;./entities/reward.entity&#39;;
import { MerchantsModule } from &#39;src/merchants/merchants.module&#39;;
@Module({
  imports: [TypeOrmModule.forFeature([RewardEntity]), MerchantsModule],
  controllers: [RewardsController],
  providers: [RewardsService],
})
export class RewardsModule {}

after I export the

  exports: [MerchantsService], 

from the merchants.module.ts as pointed out by @Wuagliono.

huangapple
  • 本文由 发表于 2023年6月5日 16:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定