FormData FileList is returnig as [Object FileList]

Viewed 466

It's a simple FileList of images.

Object.keys(pet).forEach((key) => {
      if (key === "images") {
        formData.append("images", pet[key]); //my fileList
      } else {
        formData.append(key, pet[key]);
      }
    });

Console.log of this FileList >> enter image description here

But when I try to access the same fileList at the back-end, it shows an empty array...

const images = req.files;
console.log(images); // equals to []

And when I tried like this:

const images = req.body.images;
console.log(images); // Returns this: [object FileList].

When I use this same endpoint with Postman works perfectly...

1 Answers

Just found out what was going on... I was passing an Object Array that contains a FileList that is anothar Array so it was never going to work...

 Object.keys(pet).forEach((key) => {
      if (key === "images") {
        let images = Array.from(pet.images[0]); //needed to access the first array first
        for (let i = 0; i < images.length; i++) {
          formData.append("images", images[i]);
        }
      } else {
        formData.append(key, pet[key]);
      }
    });
Related