Har*_*ain 5 circular-dependency forward-reference mongoose nestjs
作者.module.ts
@Module({
imports: [
forwardRef(() => ArticleModule),
MongooseModule.forFeature([{ name: Author.name, schema: AuthorSchema }]),
],
controllers: [AuthorController],
providers: [AuthorService, AuthorsRepository, UtilService],
exports: [AuthorService],
})
export class AuthorModule {}
Run Code Online (Sandbox Code Playgroud)
文章.module.ts
@Module({
imports: [
forwardRef(() => AuthorModule),
CityModule,
// BookmarkModule,
MongooseModule.forFeature([{ name: Article.name, schema: ArticleSchema }]),
],
controllers: [ArticleController],
providers: [ArticleService, ArticlesRepository],
})
export class ArticleModule {}
Run Code Online (Sandbox Code Playgroud)
author.schema.ts(部分文件)
@Schema(DefaultSchemaConfig)
export class Author extends BaseSchema {
@ApiPropertyOptional()
@IsString()
@Prop()
name?: string;
@ApiPropertyOptional({ type: ArticleRefDto, isArray: true })
@IsArray()
@Prop({ type: [{ type: MongooseSchema.Types.ObjectId, ref: 'Article' }] })
articles?: ArticleRefDto[];
}
export type AuthorDoc = Author & Document;
export const AuthorSchema = SchemaFactory.createForClass(Author);
Run Code Online (Sandbox Code Playgroud)
文章.schema.ts
@Schema(DefaultSchemaConfig)
export class Article extends BaseSchema {
@ApiProperty()
@IsString()
@Prop({ required: true })
title!: string;
@ApiPropertyOptional({ type: AuthorRefDto })
@Prop({
type: MongooseSchema.Types.ObjectId,
ref: 'Author',
index: true,
sparse: true,
})
author?: AuthorRefDto;
}
export type ArticleDoc = Article & Document;
export const ArticleSchema = SchemaFactory.createForClass(Article);
Run Code Online (Sandbox Code Playgroud)
我需要在作者模式中引用articledto,反之亦然。但它在其中形成了循环依赖并抛出错误
class ArticleDto extendsarticle_schema_1.Article { ^
类型错误:类扩展值未定义不是构造函数或 null
我尝试了前向引用,但它不起作用,仍然显示相同的错误。我已经在模式中尝试了简单的 ref 方法,没有forwardref 仍然抛出相同的错误。
我需要在两个模式中进行双重映射,并尝试我能找到的所有可能的解决方案。如果您需要解决方案的更多信息,请告诉我
| 归档时间: |
|
| 查看次数: |
517 次 |
| 最近记录: |