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

147
Vistas
the id of cart shows null in controller but i m sure i m sending it NODE JS

I m sending the id of the product from this view (I m using express and mongoose db)

    <form action="/cart" method="POST">
      <input type="hidden" name="id" value="<%= product._id %>" />
      <button class="btn" type="submit">Add to Cart</button>

I m sure it is not null here

then I have this middleware to receive it

router.post("/cart", shopController.postCart);

and here is the function post Cart

exports.postCart = (req, res, next) => {
  const prodId = req.params.id;
  console.log("id returned from the params : " + prodId);
  Product.findById(prodId)
    .then((product) => {
      console.log(product);
      return req.user.addToCart(product);
    })

When I first receive it in the postcard function I made a log and it gives me the log console

id returned from the params: undefined
null

I tried wrapping the id with ObjectId of mongoose then it shows me this message

exports.postCart = (req, res, next) => {
  const prodId = req.params.id;
  console.log("id returned from the params : " + ObjectId(prodId));
  Product.findById(ObjectId(prodId))
    .then((product) => {
      console.log(product);
      return req.user.addToCart(product);
    })

error :

id returned from the params : 62c9a1ca3b17ea86134d12fc
null

product: in database :

{  "_id": {    "$oid": "62c87c728594df06fd459521"  },  "title": ",gh,",  "price": 44,  "description": "nfggfngh ",  "imageUrl": "https://media.gettyimages.com/photos/stack-of-books-picture-id157482029?s=612x612",  "userId": {    "$oid": "62c71d0ab0312958a8082d6f"  },  "__v": 0}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Replace const prodId = req.params.id; with const prodId = req.body.id; and write

router.post("/cart", express.urlencoded({extended: false}), shopController.postCart);

The express.urlcoded middleware is needed to fill the req.body.

about 4 years ago · Juan Pablo Isaza Denunciar
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