when a user login and clicks the cart button i want to see the products which added by the user in my console. in my cart collection there is some documents which contain userid and products id.so when a user clicks the cart link on the navbar i want to see the products which added to the cart by the user in my console but instead of this am getting this error.this is the first time i using aggregate in monogodb
getCartProduct in usehelpers.js
getCartProducts: userId => {
return new Promise(async (resolve, reject) => {
let CartItems = await db
.get()
.collection(Collection.CART_COLLECTIONS)
.aggregate([
{
$match: { user: objectId(userId) },
},
{
$lookup: {
from: Collection.PRODUCT_COLLECTIONS,
let: { prodList:'$products' },
pipeline: [
{
$match: {
$expr: {
$in: ['$_id', '$$prodList'],
},
},
},
],
as: 'CartItems',
},
},
])
.toArray();
resolve(CartItems);
});
},
};
arguments passed from user.js
router.get('/cart',verifyLogin,async(req,res)=>{
let products = await userHelpers.getCartProducts(req.session.user._id)
console.log(products);
res.render('user/cart')
})
console error
MongoServerError: PlanExecutor error during aggregation :: caused by :: $in requires an array as a second argument, found: objectId
at Connection.onMessage (C:\Users\krish\OneDrive\Desktop\project mern stack\webdev-challenge\node_modules\mongodb\lib\cmap\connection.js:210:30)
at MessageStream.<anonymous> (C:\Users\krish\OneDrive\Desktop\project mern stack\webdev-challenge\node_modules\mongodb\lib\cmap\connection.js:63:60)
at MessageStream.emit (events.js:400:28)
at processIncomingData (C:\Users\krish\OneDrive\Desktop\project mern stack\webdev-challenge\node_modules\mongodb\lib\cmap\message_stream.js:132:20)
at MessageStream._write (C:\Users\krish\OneDrive\Desktop\project mern stack\webdev-challenge\node_modules\mongodb\lib\cmap\message_stream.js:33:9)
at writeOrBuffer (internal/streams/writable.js:358:12)
at MessageStream.Writable.write (internal/streams/writable.js:303:10)
at Socket.ondata (internal/streams/readable.js:731:22)
at Socket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:293:12)