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

246
Views
¿Cómo empujar a una matriz en una operación asíncrona en JavaScript?

Estoy tratando de empujar a una matriz dentro de un bucle forEach anidado en JavaScript pero sin éxito. He intentado clonar la matriz y agregarla, pero parece que no funciona para mí. a continuación es mi código. cualquier ayuda sería muy apreciada. gracias de antemano.

 const trollImgIds = ["61c25b801957f9eb4f5c2a94", "61c25b801957f9eb4f5c2a95"]; const images:any = []; trollImgIds.forEach((trollImgId:any) => { const imgIds = trollImgId.imgIds; imgIds.forEach(async (imgId:any) => { const _id = new mongoose.Types.ObjectId(imgId); imgGfs.find({ _id: _id }).toArray((err:any, files:any) => { if(err){ console.log(err); } if(files.length > 0){ const image:any = []; // open download stream and add images to the images array const readstream = imgGfs.openDownloadStream(_id); // read buffer using stream readstream.on('data', (chunk:any) => { image.push(chunk); }); readstream.on('error', () => { console.log('error'); }); readstream.on('end', () => { // convert buffer to bufferlist const bufferList = new BufferList(image); // convert the bufferlist to base64 string const base64 = bufferList.toString('base64'); // add to images array // console.log("image >>> : ", base64); // clone and add to images array const trollImage = { trollId: trollImgId.id, image: base64 } // clone the images array const cloneImages = [...images]; // add the troll image to the images array cloneImages.push(trollImage); // set the images array images.push(cloneImages); }); } }); }); });

Esperaba obtener una salida como:

 images >>> : [ { trollId: new ObjectId("61c25b801957f9eb4f5c2a94"), image: '/9j/4AAQSkZJRgABAQEAYABgAAD/...' } ]

que es el caso cuando lo registro dentro del segundo bucle forEach, sin embargo, resulta estar vacío fuera de los bucles

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