根据填充元素计算自定义 属性
Calculate custom property based on populate element
我有两个模式:配方和产品
var recipeSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
products: [{
product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' },
productWeight: Number,
productPrice: Number
}]
})
var productSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
weight: {
type: Number,
required: true
},
price: {
type: Number,
required: true
}
})
而且我有 addRecipe 功能
module.exports.addRecipe = function(req, res){
Recipe
.create({
name: req.body.name,
products: req.body.products
}, function(err, recipe){
if(err){
console.log("Error creating recipe");
res
.status(400)
.json(err);
} else {
console.log("Recipe created", recipe);
res
.status(201)
.json(recipe);
}
})
}
我想为数组中的每个对象计算产品价格 (productPrice = product.price * productWeight / product.Weight)。
我的帖子 JSON.
{
"name": "Cake",
"products": [
{
"product": "59728a3f7765441b503e31bc",
"productWeight": 100
},
{
"product": "59728a3f7765441b503e31bd",
"productWeight": 200
},
{
"product": "59728a3f7765441b503e31be",
"productWeight": 50
},
{
"product": "59728a3f7765441b503e31bf",
"productWeight": 500
}
]
}
我还想更新产品的 productPrice 或 reicpe 编辑。
可以这样做吗?
谢谢!
我会为此使用 virtual,但我认为如果不引入另一个模式就无法使用它。
var RecipeProductSchema = new mongoose.Schema({
product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' },
productWeight: Number
});
// note: product must be populated before calling this property
RecipeProductSchema.virtual('productPrice').get(function() {
return this.product.price * this.productWeight / this.product.weight;
});
var RecipeSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
products: [RecipeProductSchema]
})
我有两个模式:配方和产品
var recipeSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
products: [{
product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' },
productWeight: Number,
productPrice: Number
}]
})
var productSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
weight: {
type: Number,
required: true
},
price: {
type: Number,
required: true
}
})
而且我有 addRecipe 功能
module.exports.addRecipe = function(req, res){
Recipe
.create({
name: req.body.name,
products: req.body.products
}, function(err, recipe){
if(err){
console.log("Error creating recipe");
res
.status(400)
.json(err);
} else {
console.log("Recipe created", recipe);
res
.status(201)
.json(recipe);
}
})
}
我想为数组中的每个对象计算产品价格 (productPrice = product.price * productWeight / product.Weight)。
我的帖子 JSON.
{
"name": "Cake",
"products": [
{
"product": "59728a3f7765441b503e31bc",
"productWeight": 100
},
{
"product": "59728a3f7765441b503e31bd",
"productWeight": 200
},
{
"product": "59728a3f7765441b503e31be",
"productWeight": 50
},
{
"product": "59728a3f7765441b503e31bf",
"productWeight": 500
}
]
}
我还想更新产品的 productPrice 或 reicpe 编辑。 可以这样做吗?
谢谢!
我会为此使用 virtual,但我认为如果不引入另一个模式就无法使用它。
var RecipeProductSchema = new mongoose.Schema({
product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' },
productWeight: Number
});
// note: product must be populated before calling this property
RecipeProductSchema.virtual('productPrice').get(function() {
return this.product.price * this.productWeight / this.product.weight;
});
var RecipeSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
products: [RecipeProductSchema]
})