首页 > 解决方案 > nestjs mongoose 自引用子文档模型类型错误

问题描述

我有一个运行猫鼬的nestjs后端。当我在纸上为我的数据库建模时,我需要一个与其自身相关的部分来表示子部分(类似于类别和子类别)。我在我的模型中做了这个代码:

@Schema()
export class Section {
  @Prop()
  title: string;

  @Prop({ type: mongoose.Schema.Types.Mixed })
  context: SurveyDocument | ListDocument;

  @Prop({ type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Section' }] })
  subSections: SectionDocument[];

  // for sub sections
  @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'Section' })
  parentSection: SectionDocument;
}

export type SectionDocument = Section & mongoose.Document;

export const SectionSchema = SchemaFactory.createForClass(Section);

这是我的服务:

@Injectable()
export class SectionService {
  constructor(
    @InjectModel(Section.name)
    private readonly sectionModel: Model<SectionDocument>,
  ) {}

  async findAll(): Promise<SectionDocument[]> {
    return this.sectionModel.find().exec();
  }
}

但是Model<SectionDocument>会给我这个错误:

Type of property 'parentSection' circularly references itself in mapped type 'LeanDocument<SectionDocument>'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type 'Omit<_LeanDocument<SectionDocument>, "populate" | "$ignore" | "$isDefault" | "$isDeleted" | "$isEmpty" | "$isValid" | "$locals" | "$markValid" | ... 43 more ... | "$isSingleNested">'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '_AllowStringsForIds<LeanDocument<SectionDocument>>'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '_LeanDocument<SectionDocument>'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '{ _id?: "_id"; __v?: "__v"; id?: "id"; title: "title"; context: "context"; subSections: "subSections"; parentSection: "parentSection"; }'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '{ _id?: "_id"; __v?: "__v"; id?: "id"; title: "title"; context: "context"; subSections: never; parentSection: "parentSection"; }'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '{ _id?: "_id"; __v?: "__v"; id?: "id"; title: never; context: never; subSections: never; parentSection: "parentSection"; }'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '{ _id?: "_id"; __v?: never; id?: "id"; title: "title"; context: "context"; subSections: "subSections"; parentSection: "parentSection"; }'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '{ _id?: "_id"; __v?: never; id?: "id"; title: never; context: never; subSections: "subSections"; parentSection: "parentSection"; }'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '{ _id?: "_id"; __v?: never; id?: "id"; title: never; context: never; subSections: never; parentSection: "parentSection"; }'.ts(2615)
Type of property 'parentSection' circularly references itself in mapped type '{ _id?: never; __v?: never; id?: never; title: never; context: never; subSections: never; parentSection: never; }'.ts(2615)

我想知道我做错了什么还是只是打字错误?

标签: node.jsmongodbtypescriptmongoosenestjs

解决方案


推荐阅读