Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
$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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda