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

88
Views
almacene múltiples consultas de mangosta como resultado en una matriz

Me gustaría pedir su ayuda, estoy tratando de buscar mis datos de mongoldb usando muchas identificaciones a través de una solicitud de axios,

Inicio una solicitud con todos los ID al nodo de la siguiente manera

 var data=new FormData() data.append("offered",offered) axios ({ url:`http://localhost:5000/items/itemsDetail`, method:"POST", data:{offered:offered}, })

Luego, en el modelo de nodo back-end, inicio las consultas de la siguiente manera

 var offered=req.body.offered var responsed=[] offered.forEach(offer=>{ item.findById([offer]) .then(result=>{ console.log(result) responsed.push(result) }) res.json({result:response})

individualmente funciona y registra el resultado individual ("console.log (resultado)"), pero cuando intento recopilarlos y enviar la [matriz de variables "respondida"] como respuesta única, obtuve un objeto vacío en los axios responden?!

¡espero que hayas entendido mi punto!

Quiero enviar consultas múltiples y las obtuve una vez.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El problema es que item.findById es una función asíncrona.

Para devolver todos los datos, debe esperar a que item.findById cada elemento.findById.

Mira el siguiente ejemplo :

 // Used for the snippet to run without error const req = { body: { offered: [ 'foo', 'bar', ], }, }; // Used to simulate the async function const item = { findById: ([d]) => new Promise((r) => setTimeout(() => r(`Response :: ${d}`), 100)), }; // Used for the snippet to run without error const offered = req.body.offered; // Syntax used to be able to use async/await syntax (async() =>  { // Call every findById and wait for them to finish const response = await Promise.all(offered.map(offer => item.findById([offer]))); console.log({ result: response, }); })();

over 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!