Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inspect FormData object with console.log [duplicate]

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.

like image 704
Mulligan Avatar asked Dec 03 '22 19:12

Mulligan


2 Answers

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);
like image 180
manikant gautam Avatar answered Dec 06 '22 09:12

manikant gautam


Try with this:

for (var d of data.entries()) {
    console.log(d); 
    // Do whatever with d
}
like image 37
Matthieu Avatar answered Dec 06 '22 09:12

Matthieu