【发布时间】:2020-11-25 14:21:45
【问题描述】:
在 NestJS API 上,我想在实体中使用模块服务来使用非数据库属性填充该实体。
就我而言,我想获取我正在检索的类别的文章数。
@Entity({ name: 'categories' })
export class Category {
constructor(private readonly service: CategoriesService) { }
@PrimaryGeneratedColumn()
id: number;
@Column()
@Index({ unique: true })
title: string;
@OneToMany(() => Article, articles => articles.category)
articles: Article[];
numberOfExercices: number;
@AfterLoad()
async countExercices() {
this.numberOfExercices = await this.service.getNumberOfArticles(this.id);
}
}
但我收到此错误:
Nest can't resolve dependencies of the GlobalPrevCategoriesService (GlobalPrevCategoriesRepository, ?).
Please make sure that the argument dependency at index [1] is available in the GlobalPrevCategoriesModule context.
我尝试了这个小变种,但我得到了同样的错误。
constructor(
@Inject(GlobalPrevCategoriesService)
private readonly service: GlobalPrevCategoriesService) { }
)
这是我的模块:
@Module({
imports: [
TypeOrmModule.forFeature([CategoriesRepository]),
ArticlesModule
],
controllers: [CategoriesController],
providers: [CategoriesService],
exports: [CategoriesService]
})
export class CategoriesModule { }
是否可以在实体中使用服务?
【问题讨论】:
标签: nestjs