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

132
Views
Dos solicitudes dentro de un servicio en express

Actualmente estoy trabajando en un proyecto en el que intento construir un servicio en Express que llama a otros dos EP externos. Tengo un problema aquí, porque express me muestra un error que no puedo entender. Pero supongo que la forma en que estoy trabajando debería estar mal. Asi que

 app.get("/items/:id", (req, res) => { return request.get({ url: `https://myapi.com/${req.params.id}`, json: true }, (error, response) => { if(error) { return res.send("Error ocurred"); } const itemDesc = request.get({ // Here I'm trying to do the second call and use it later url: `https://myapi.com/${req.params.id}/description`, json: true }, (error, responseDesc) => { return responseDesc }); const itemDetails = response.body; const strPrice = itemDetails.price.toString().split('.'); const numberPrice = parseInt(strPrice[0]); const floatPrice = strPrice[1] ? parseInt(strPrice[1]) : 00; return res.send({ id: itemDetails.id, title: itemDetails.title, price: { currency: itemDetails.currency_id, amount: numberPrice, decimals: floatPrice, }, picture: itemDetails.pictures[0].url, condition: itemDetails.condition, free_shipping: itemDetails.shipping.free_shipping, sold_quantity: itemDetails.sold_quantity, description: itemDesc // Here I'm using the variable of the previous request }); }); });

Básicamente, el error que recibo es que no puedo hacer dos llamadas. Lo sé porque si elimino la solicitud anidada, funciona. El error que me sale es el siguiente: ingrese la descripción de la imagen aquí

Mi pregunta es: ¿Hay alguna forma de hacer dos solicitudes externas dentro del mismo método? Gracias por adelantado

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

es más limpio si lo hace con async await en su caso. modifica tu código así

 app.get("/items/:id", async(req, res) => { try { const promise1 = fetch(`https://myapi.com/${req.params.id}`).then(data => data.json()) const promise2 = fetch(`https://myapi.com/${req.params.id}/description`) const [itemDetails, itemDesc] = await Promise.all([promise1, promise2]) const strPrice = itemDetails.price.toString().split('.'); const numberPrice = parseInt(strPrice[0]); const floatPrice = strPrice[1] ? parseInt(strPrice[1]) : 00; res.send({ id: itemDetails.id, title: itemDetails.title, price: { currency: itemDetails.currency_id, amount: numberPrice, decimals: floatPrice, }, picture: itemDetails.pictures[0].url, condition: itemDetails.condition, free_shipping: itemDetails.shipping.free_shipping, sold_quantity: itemDetails.sold_quantity, description: itemDesc // Here I'm using the variable of the previous request }); } catch ( res.send("Error ocurred") ) });
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!