admin管理员组文章数量:1402836
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.
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.
1 Answer
Reset to default 5a quik example that describe your case is:
import { Document, Schema as MongooseSchema } from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
class GuildMember {
@Prop({ type: String, required: true, lowercase: true })
_id: string;
@Prop({ required: true })
id: number;
@Prop({ required: true })
rank: number;
}
@Schema({ timestamps: true })
export class Guild extends Document {
@Prop({ type: String, required: true, lowercase: true })
_id: string;
@Prop({ type: MongooseSchema.Types.Array})
members: GuildMember[]
}
export const GuildsSchema = SchemaFactory.createForClass(Guild);
because in nested schema you don't have yo define type INSIDE the prop decorator but only tell that this field is an array and validate the type using TypeScript
本文标签: javascriptHow to add nested array of objects with Prop decorator from nestjsmongooseStack Overflow
版权声明:本文标题:javascript - How to add nested array of objects with @Prop decorator from @nestjsmongoose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744348170a2601888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论