Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
$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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda