Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
$in requires an array as a second argument, found:

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)
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!