0

I am trying to push to an array inside a nested forEach loop in JavaScript yet to no avail. I have tried cloning the array and appending to it but doesn't seem to work for me. below is my code. any help would be hugely appreciated. thanks in advance.

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);
            });
         }
      });
  });

});

I was expecting to get an output like:

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

which is the case when I log it inside the second forEach loop however it turns out to be empty outside the loops

ikhvjs
  • 5,316
  • 2
  • 13
  • 36
IBRAHIM ALI MUSAH
  • 831
  • 10
  • 19
  • You didn't provide `trollImgIds` and an expected output. No one can understand what you try to achieve. – ikhvjs Dec 22 '21 at 13:43
  • 1
    It's a hard example of code. You should simplify when exposing a doubt. Try to read this [post](https://stackoverflow.com/questions/12482961/change-values-in-array-when-doing-foreach): – Gui Dec 22 '21 at 13:44
  • What does this mean, please be precise: "_doesn't seem to work for me._" – Randy Casburn Dec 22 '21 at 13:46
  • What you try to do is push an array in async operation. You may have a look of this question. https://stackoverflow.com/questions/54100855/pushing-to-an-array-in-async-function-not-working – ikhvjs Dec 22 '21 at 14:01
  • Based upon your edit, [this is your issue](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Randy Casburn Dec 22 '21 at 14:01
  • Thanks man, works like a charm. I switched to nested for-loop and added a callback function and it worked fine. thanks brother(s) – IBRAHIM ALI MUSAH Dec 22 '21 at 16:11

0 Answers0