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

148
Views
la identificación del carrito muestra nulo en el controlador, pero estoy seguro de que lo estoy enviando NODE JS

Estoy enviando la identificación del producto desde esta vista (estoy usando express y mongoose db)

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

Estoy seguro de que no es nulo aquí

entonces tengo este middleware para recibirlo

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

y aquí está la función 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); })

Cuando lo recibo por primera vez en la función de postal hice un registro y me da la consola de registro

 id returned from the params: undefined null

Intenté envolver la identificación con ObjectId de mongoose y luego me muestra este mensaje

 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

producto: en la base de datos:

 { "_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 answers
Answer question

0

Reemplace const prodId = req.params.id; con const prodId = req.body.id; y escribe

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

Se necesita el middleware express.urlcoded para llenar el req.body .

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