node.js - 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)
我想知道我做错了什么还是只是打字错误?
解决方案
推荐阅读
- rust - 将 FnMut 存储在结构中会产生生命周期问题
- python - 如何循环一个输入而不是重新启动整个程序?
- node.js - Mysql插入带有来自嵌套值的动态值
- rust - 使用 Rc 时的终身问题
> 实现二叉搜索树 - c++ - 如何将 constexpr 字符串数组作为参数传递?
- gdb - 如何将 ELF 文件的内容转储到特定地址?
- html - 如何将元素放置在输入字段的左侧?
- python - ImportError:无法从部分初始化的模块导入名称“twiml”
- python - Python:从父包子类化一个类有什么问题?
- javascript - Highcharts- Javascript- 默认情况下如何显示所有图例系列的累积计数,以及图例中的链接系列