首页 > 解决方案 > 使用具有正确参考文档的 mongo id 数组创建 dto 属性

问题描述

我在 Nestjs 应用程序中有这个创建 DTO

class CurrencyItem {
  @IsMongoId()
  @Prop({ type: MongooseSchema.Types.ObjectId, ref: Currency.name })
  currency_id: string;
}

export class CreateQuotationDto {
  @IsNotEmpty()
  quote_name: string;

  @IsNotEmpty()
  @IsMongoId()
  @Prop({ type: MongooseSchema.Types.ObjectId, ref: Supplier.name })
  supplier: string;

  @IsNotEmpty()
  @IsMongoId()
  @Prop({ type: MongooseSchema.Types.ObjectId, ref: Currency.name })
  currency: string;

  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => CurrencyItem)
  conversion_currencies: CurrencyItem[];

  shipping_company: string;
  shipping_rate: number;
  custom_charges: number;

  @IsNotEmpty()
  @Type(() => OtherCharges)
  other_charges: OtherCharges[];

  @IsNotEmpty()
  @Type(() => QuoteItem)
  items: QuoteItem[];

  date: number;
}

我想做的第一件事是从对象数组中删除 currency_id 并且只想要一个 mongo id 数组,例如:

{
    "quote_name": "batch 1",
    "supplier": "60a25a84ef72ff68d8e467c6",
    "currency": "60a39051ad18d0747e51bfd5",
    "conversion_currencies": ["60a25a84ef72ff68d8e467c6", "60a39051ad18d0747e51bfd5"],
    "other_charges": "23",
    "items": "123"
}

我还想验证我保存的 id 文件是否存在于货币集合中?到目前为止,我能够在对象数组中验证 mongo id

任何帮助也将不胜感激,@Prop验证在这里不起作用

标签: mongodbmongoosenestjsclass-validator

解决方案


推荐阅读