我有一個產品列表,每個產品都有自己的選項。例如:填充另一個對象引用的對象
- 藍色禮服(S - L)
- 紅服飾(XS - S - M)
藍色禮服和紅服飾是產品,小號 ,L,XS,S和M是選項。選項模型有對產品模型的參考,我想檢索所有產品,然後列出他們自己的選項。
我想用一個查詢來實現它,我的問題是我從沒有鏈接到它的選項的產品開始。所以我開始找到所有的產品,並與一個嵌套的然後與foreach循環我得到它的所有選項。然後我嘗試將該選項分配給產品對象(在我的情況下,產品Ellem內部的for each),但是當我檢索出它的範圍當然是空的。如何填充從產品查詢開始的選項?
產品架構:
var schema = new Schema({
imagePath: {type: String},
title: {type: String, required: true},
description: {type: String, required: true}
});
選項模式:
var productOptionSchema = new Schema({
type: {type: String, enum: ['grams'], default: 'grams', required: true},
value: {type: String, required: true},
price: {type: Number, required:true},
product: {type: Schema.Types.ObjectId, ref: 'User', required:true}
});
在這裏,我試圖尋找產品
router.get('/shop/products/list', isLoggedIn, function (req, res, next) {
Product.find()
.then(function (products) {
products.forEach(function(productElem) {
ProductOption.find({product: productElem._id})
.then(function (options) {
productElem['options'] = [];
options.forEach(function(optionElem) {
productElem['options'].push(optionElem);
});
});
});
res.render('shop/listProduct', {user:req.user, csrfToken: req.csrfToken(), messages:messages, partialCustom:
});
})
.catch(function (err) {
console.log('Error ' + err.code + ': ', err.message);
res.status(500).send('Failed to get the Product List from the DB: ' + err);
});
});
你的問題不是很清楚。請編輯該問題以使其更易於理解。 –