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

300
Views
Restablecer el código de estado de retorno de la API 201 incluso con un "error interno"

Cuando realiza una solicitud POST sin datos del cuerpo, debe devolver un código de estado 400, pero está devolviendo un código de estado 201 con un objeto vacío.

Este el código:

 router.post('/', (req: Request, res: Response) => { const transaction = new Transactions({ name: req.body?.name, amount: req.body?.amount, type: req.body?.type, }) try{ const newTransaction = transaction.save(); return res.status(201).json(newTransaction); }catch (err){ return res.status(400).json({message: err.message}); } })

Esta es la solicitud del cartero y la respuesta:

Solicitud de cartero

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Esto se parece a mongodb y mangosta. .save() devuelve una promesa y debe resolverse. Puedes hacerlo con async/await aquí. Lo que devuelves como respuesta es una promesa pendiente.

 router.post("/", async (req: Request, res: Response) => { const transaction = new Transactions({ name: req.body?.name, amount: req.body?.amount, type: req.body?.type, }); try { const newTransaction = await transaction.save(); return res.status(201).json(newTransaction); } catch (err) { return res.status(400).json({ message: err.message }); } });
about 4 years ago · Santiago Gelvez 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!