toArray不是mongodb中的函数,mongoose

bn31dyow  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(141)

to Array不是mongo数据库中的函数,mongoose,node.js

`getCartProducts: (userId) => {
        return new Promise(async (resolve, reject) => {
            let cart Items = await db.cart.aggregate([

                {`your text`
                    $match: { user: user Id }//matched with the id
                },
                {
                    $lookup: {
                        from: "db.products",
                        let: { proList: '$products' },
                        pipeline: [
                            {
                                $match: {
                                    $expr: {
                                        $in: ["$_id", '$$proList']
                                    }
                                }
                            }
                        ],
                        as: 'cart Items' //converted as cart Items name
                    }
                }

            ]).`to array`()
            resolve(`cart Items`)
        })
    }

db.cart.aggregate().to Array不是一个函数,我试图删除to数组,但它显示为未定义

rjee0c15

rjee0c151#

问题是您正在对promise执行toArray(),它应该是这样的..您不需要创建自定义promise..

const getCartItems = () => {
  // ...
  const items = await db.cart.aggregate([...])
  return items.toArray();
  ...
}

相关问题