MAHESWAR DILEEP
MAHESWAR DILEEP

Reputation: 15

toArray is not a function in mongodb,mongoose

enter image description here

to Array is not a function in mongo database, 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 is not a function I tried to remove the to array but it shows as un defined

Upvotes: 0

Views: 568

Answers (1)

Suraj Tiwari
Suraj Tiwari

Reputation: 386

Issue is you're doing toArray() on promise, it should be something like this.. you don't need to create custom promise..

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

Upvotes: 2

Related Questions