4

I've created FormData object created like that:

var data = new FormData()
data.append('image', input[0].files[0])
data.append('user', 'hubot')

Now when i try to inspect it like that

console.log(data)

i get empty FormData object (the only propery is __proto__). There is no image and user properties.

Mulligan
  • 202
  • 1
  • 3
  • 9

2 Answers2

15

You should have to iterate over entries . FormData is type of object so rather than logging itself you have to get values by get method or iterate over entries . Here is how.

var data = new FormData()
//data.append('image', input[0].files[0])
data.append('user', 'hubot')
   
for(var pair of data.entries()) {
   console.log(pair[0]+ ', '+ pair[1]); 
}
  

If you want to fetch only one value at a time , you can have by key

  console.log(data.get('user'));
  // 'hubot'

Edit

Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected. More from here . So if you want to simply log

   // console.log(...data);
manikant gautam
  • 3,521
  • 1
  • 17
  • 27
  • Ok, thx. it works. But my question is now why does simple `console.log` works with objects like `user = {name: "John"}` but doesn't with FormData? – Mulligan Oct 25 '18 at 09:45
1

Try with this:

for (var d of data.entries()) {
    console.log(d); 
    // Do whatever with d
}
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Matthieu
  • 151
  • 1
  • 9
  • Ok, thx. it works. But my question is now why does simple console.log works with objects like user = {name: "John"} but doesn't with FormData? – Mulligan Oct 25 '18 at 15:43