我是node.js的新手,并处于以下情况。
我需要获取产品和与所获取产品具有相同类别的所有其他产品的详细信息。请有人告诉我如何将同一类别的主要产品详细信息和相关产品传递给视图。
浏览器网址
http://localhost:3000/product/dragon_road/5b30ca61c2efd01318915afe
型号代码:
var ProductSchema = new Schema({
category: {
type: mongoose.Schema.Types.ObjectId, ref: 'Category'
},
user: {
type: mongoose.Schema.Types.ObjectId, ref: 'User'
},
name: String,
overview: String,
discount: String,
discount_price: Number,
actual_price: Number,
image: String,
author: String,
condition: {type: String, enum: ['New', 'Old', 'Old as New']},
sell_mode: String,
star_rating: {type: Number, default: 1},
reviews: {type: Number, default: 0},
like: {type: Number, default: 0},
dislike: {type: Number, default: 0},
featured: {type: Boolean, defualt: false},
createdAt: Date,
updatedAt: Date
});
主要代码:
router.get('/product/:prd_name/:id', function (req, res, next) {
console.log(req.params.id);
console.log(req.params.prd_name);
var mainProductObject = Product.findById({_id: req.params.id}).populate('category');
res.render('main/product', {
product: product,
related_products: related_products
});
});