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

232
Views
¿El archivo JS no extrae model.findByPk () usando "/: id"?

Estoy tratando de extraer elementos de mi base de datos usando la id de cada elemento, pero recibo un objeto vacío cuando lo ejecuto a través de Insomnia. Por ejemplo, en el código a continuación, me gustaría extraer una categoría por ID, pero también incluir cualquier Producto asociado. ¿Alguna idea de lo que podría estar haciendo mal? ¡Gracias de antemano!

 router.get('/:id', async (req, res) => { try { const oneCategory = await Category.findByPk({ include: [{ model: Product }] }); // console.log(oneCategory); if (!oneCategory) { res.status(404).json({ message: 'No category found with that id!' }); return; } res.status(200).json(oneCategory); } catch (error) { res.status(500).json(error); } });

Objeto Vacío en Insomnio

Tabla de categorías en DB

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Al llamar al método findByPk , debe pasar la clave que está buscando. En este caso particular, el código debería verse así:

 router.get('/:id', async (req, res) => { try { const oneCategory = await Category.findByPk(req.params.id, { include: [{ model: Product }] }); if (!oneCategory) { res.status(404).json({ message: 'No category found with that id!' }); return; } res.status(200).json(oneCategory); } catch (error) { res.status(500).json(error); } });

Así que solo tome la identificación de la URL con req.params.id y pásela a findByPk . Sin embargo, podría ser una buena idea verificar que la identificación sea un número entero antes de hacerlo :)

about 4 years ago · Santiago Trujillo 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!