【问题标题】:How to define dynamic key in object in array of objects with @Prop() decorator in schema? Mongoose, NestJS如何在模式中使用 @Prop() 装饰器在对象数组中定义动态键?猫鼬,NestJS
【发布时间】:2022-10-06 19:51:21
【问题描述】:
我有这样的对象数组
const array = [
{key1: value1},
{key2: value2},
...
]
如何使用 @Prop 装饰器在模式中正确定义它?
@Schema()
export class Entity{
@Prop() // Here
body: ; // and here
}
标签:
typescript
mongoose
nestjs
nestjs-mongoose
【解决方案1】:
这样的事情会有帮助吗?
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
export type UserDocument = User & Document;
// Any key as string, and value as a number.
export interface CustomData {
[key: string]: number;
}
@Schema()
export class User {
@Prop()
name: string;
@Prop()
age: number;
@Prop({ type: [Object] })
customData: CustomData[];
}
export const UserSchema = SchemaFactory.createForClass(User);