在猫鼬中填充嵌套数组不起作用

问题描述

所需的输出应该是被引用的产品,但是我只得到产品的_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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...