所需的输出应该是被引用的产品,但是我只得到产品的_id
router.get('/',async (req,res)=>{
try {
const ordersGet =await OrderModel.find({}).populate("orders.product").exec()
console.log(ordersGet[0].orders[0])
//this outputs { _id: 5f88e552794e0b1bb0a4b99d, quantity: 1 }
// { _id: 5f88e552794e0b1bb0a4b99d, quantity: 1 }
//I want to get something like this { product: {referenced product doc}, quantity: 1 }
if(ordersGet[0] == undefined) throw new Error('no orders')
res.json(ordersGet)
} catch (error) {
res.sendStatus(400).send('no orders')
}
})
这是我的订单模型
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const OrderSchema = new Schema(
{
orders:[
{
product :{
type: Schema.Types.ObjectId,
ref: 'Product'
},
quantity:Number,
}
],
clientId : {
type: Schema.Types.ObjectId,
ref: 'User'
},
date:Date,
cost:Number,
billing_details:{}
}
)
module.exports = mongoose.model('Order', OrderSchema);