问题描述
所需的输出应该是被引用的产品,但是我只得到产品的_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);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)