MiguelG
MiguelG

Reputation: 456

Mongoose Nestjs, implement recursive subdocument array

I have the following type:

export type TreeViewBaseItem = {
    id: string;
    label: string;
    editable:boolean;
    children?: TreeViewBaseItem[];
    files: TreeFile[];
};

and I did my best to create the following subdocument in nestjs:

@Schema()
export class TreeViewBaseItem {
  @Prop({ type: String, required: true })
  id;
  @Prop({ type: String, required: true })
  label;
  @Prop({ type: [TreeViewBaseItem], required: false })
  children: TreeViewBaseItem[];

  @Prop({ type: Boolean, required: false })
  editable;

  @Prop({ type: [TreeFile], required: false })
  files: TreeFile[];
}

Now when using this schema in a top level document:

@Schema()
export class Project {
...

  @Prop({ type: [TreeViewBaseItem], required: true, default: [] })
  folderStructure: TreeViewBaseItem[];
...
}

I get the following error in the console:

RangeError: Maximum call stack size exceeded
at DefinitionsFactory.inspectTypeDefinition
at Array.forEach
at DefinitionsFactory.createForClass

It's all related to the recursive property children: TreeViewBaseItem[]

What it should be the appropriate way to implement a recursive subdocument array? The TreeViewBaseItem is part of a tree view folder feature that I need to store in the project document so the folder structure persist in the user application.

Upvotes: 1

Views: 16

Answers (0)

Related Questions