When I use a nested array of object in prop decorator:
@Schema()
export class Child {
@Prop()
name: string;
}
@Schema()
export class Parent {
@Prop({type: [Child], _id: false}) // don't need `_id` for nested objects
children: Child[];
}
export const ParentSchema = SchemaFactory.createForClass(Parent);
I get an error:
TypeError: Invalid schema configuration: `Child` is not a valid type within the array `children`.
How can I fix this if I need to use @Prop({_id: false})
(to keep the nested schema independent)?
If we change a prop decorator to @Prop([Child])
it works, however we need to disable _id
for nested object with:
@Schema({_id: false})
export class Child {
@Prop()
name: string;
}
@Schema()
export class Parent {
@Prop([Child])
children: Child[];
}
And in this case we won't have generic Child object and we won't to use them as an independent Schema.
Another way is to create Child
schema and use it in @Prop({type: [childSchema], _id: false})
, but that looks like an overhead.