【发布时间】:2021-09-27 21:41:45
【问题描述】:
我正在尝试编写一个 Eshop 应用程序,我想使用 virtuals
获得产品总价乘以产品数量这是我的 "cart-item" 架构
const cartItem = new Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
},
products: [productSchema],
},
{ timestamps: true }
);
//Product Schema
const productSchema = new Schema({
productId: { //ref to Product Schema, [simple schema with name, price and properties ]
type: mongoose.Schema.Types.ObjectId,
ref: "product",
},
quantity: {
type: Number,
default: 1,
required: true,
},
});
productSchema.virtual("price").get(function () {
const price = **// How to get price from "productId" and return as virtual**
return "virtual";
});
如何获取产品价格并从虚拟中获取?
【问题讨论】:
标签: javascript node.js mongoose mongoose-schema mongoose-populate