node.js - NestJs:从另一个模块导入服务
问题描述
我正在尝试使用surveyService
in voteOptionRepository
,但是当我使用路由时,控制台会返回:TypeError: this.surveyService.getSurveyById
is not a function
这是我的调查模块
@Module({
imports: [
TypeOrmModule.forFeature([SurveyRepository]),
AuthModule
],
controllers: [SurveyController],
providers: [SurveyService],
exports: [SurveyService]
})
export class SurveyModule{}
这是我的 voteOptionModule
@Module({
imports: [
TypeOrmModule.forFeature([VoteOptionRepository]),
AuthModule,
SurveyModule
],
controllers: [VoteOptionController],
providers: [VoteOptionService]
})
export class VoteOptionModule{}
这就是我尝试使用该服务的方式
@EntityRepository(VoteOption)
export class VoteOptionRepository extends Repository<VoteOption>{
constructor(private surveyService: SurveyService){
super();
}
async createVoteOption(createVoteOptionDTO: CreateVoteOptionDTO, surveyId: number, user: User){
const survey = await this.surveyService.getSurveyById(surveyId, user)
const { voteOptionName, image } = createVoteOptionDTO;
const voteOption = new VoteOption();
voteOption.voteOptionName = voteOptionName;
voteOption.image = image;
voteOption.survey = survey;
try{
await voteOption.save()
this.surveyService.updateSurveyVoteOptions(voteOption, surveyId, user)
} catch(error){
throw new InternalServerErrorException();
}
delete voteOption.survey;
return voteOption;
}
}
解决方案
我是 NestJS 的新手,但我相信这是你想要做的。您需要在服务中注入对表的引用。
export class VoteOptionRepository extends Repository<VoteOption>{
constructor(
private surveyService: SurveyService
@InjectRepository
surveyServiceRepo(SurveyRepository)
){
super();
}
async getSurveyById () {
const survey = await this.surveyServiceRepo.find({id: 'survey-id'})
return survey
}
}
但是,如果没有看到您的 SurveyService,可能是getSurveyById
没有正确定义,这就是为什么您会收到错误消息,指出它不是函数。
推荐阅读
- android - 从 uri 获取数据列的值时出现 IllegalArgumentException
- swagger - 使用什么注释来定义 HTTP 请求标头?
- rust - Rust 编译器是否将 String 隐式转换为 &str?
- c++ - 从 file.cpp 映射到 file.o cmake
- revit-api - C# Revit API,如何使用 ExternalCommand 创建简单的墙?
- python - 无法安装 Pyaudio
- shell - 在 awk 中比较 2 个文件
- javascript - 我如何在nodeJS中比较来自mongodb的日期
- python - 在字典列表中查找键值
- c# - 以特定语言格式化值的任何方式