英文:
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'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 '@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.findOneBy({
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 {}
答案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 '@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], // <-- 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 '@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 {}
after I export the
exports: [MerchantsService],
from the merchants.module.ts as pointed out by @Wuagliono.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论