在mongoose中填充,如何设置要由字段填充的架构名称



我有一个产品模式和订单模式,我试图在订单模式中填充产品信息,但只得到了产品id的字符串。i将产品id作为字符串i发布到订单模型,也许我应该将其保留为ObjectId?如果我这样做,怎么做?

产品展示:

export type ProductDocument = Product & Document;
@Schema({timestamps: true, versionKey: false})
export class Product{
@Prop()
title:string;
@Prop()
price: number;
@Prop()
image: string;
@Prop()
description: string;
@Prop()
totalAmount: number;
@Prop()
counter: string;
@Prop()
availableAmount: number;
@Prop()
originalPrice: number;
}
export const ProductSchema = (SchemaFactory.createForClass(Product))

订单模式:

export type OrdersDocument = Orders & Document;
@Schema({timestamps: true})
export class Orders {
@Prop()
userId: string;
@Prop({
type: mongoose.Schema.Types.ObjectId, ref: Product.name,
})
productId: Product;
@Prop()
amount: number;
}
export const OrdersSchema = SchemaFactory.createForClass(Orders);

我以这种方式将模型导入订单模块

MongooseModule.forFeature([{ name: Product.name, schema: ProductSchema }])

我找到了解决方案,但我没有在订单中使用.popuple('productId(。service

最新更新