|
| 1 | +# Why is my `FormData` empty? |
| 2 | + |
| 3 | +## The problem |
| 4 | + |
| 5 | +Let's imagine you've created a [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) object: |
| 6 | + |
| 7 | +```js |
| 8 | +const formData = new FormData() |
| 9 | +formData.append('product', 'id-1234') |
| 10 | +``` |
| 11 | + |
| 12 | +The specifics don't matter too much, but clearly `formData` should not be empty, it should contain an entry for `product`. |
| 13 | + |
| 14 | +If you try to check the contents of `formData` in the console: |
| 15 | + |
| 16 | +```js |
| 17 | +console.log(formData) |
| 18 | +``` |
| 19 | + |
| 20 | +You'll likely see something like this: |
| 21 | + |
| 22 | +``` |
| 23 | +▼ FormData {} |
| 24 | + ▶ [[Prototype]]: FormData |
| 25 | +``` |
| 26 | + |
| 27 | +It appears to be empty. |
| 28 | + |
| 29 | +Using `JSON.stringify()` doesn't help either: |
| 30 | + |
| 31 | +```js |
| 32 | +console.log(JSON.stringify(formData)) |
| 33 | +``` |
| 34 | + |
| 35 | +Output: |
| 36 | + |
| 37 | +``` |
| 38 | +{} |
| 39 | +``` |
| 40 | + |
| 41 | +You could try outputting the object in a component template using <code v-pre>{{ formData }}</code>, but that still doesn't show the `product`. |
| 42 | + |
| 43 | +## The solution |
| 44 | + |
| 45 | +The `formData` object isn't empty, but none of the techniques we've used so far will show that. |
| 46 | + |
| 47 | +`FormData` exposes the methods [`keys()`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/keys), [`values()`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/values) and [`entries()`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/entries) that we can use to inspect its contents. For example: |
| 48 | + |
| 49 | +```js |
| 50 | +console.log(Object.fromEntries(formData.entries())) |
| 51 | +``` |
| 52 | + |
| 53 | +Now we'll get the output we wanted: |
| 54 | + |
| 55 | +``` |
| 56 | +{product: 'id-1234'} |
| 57 | +``` |
| 58 | + |
| 59 | +Here's a Playground example to demonstrate this in action: |
| 60 | + |
| 61 | +- [Playground example](https://play.vuejs.org/#eNqNks1uwjAQhF/F8iUgUaMWTihF6g+VygGq0qMvabIJoYlt2RugivLutQ2BtGpRb9mZb1ez3tT0Tim2rYBOaGhinSskBrBSUy5iKQySVOryMcKI3BIBO/J0LHt9LrhoTRYpBSLpBUrLpIoxGJAgT66ub0bjwINuliyAFTLrtU3W6Mrz1XLBDOpcZHn6eaZ+YMv3DcTIUi3LmbA0mBPK4Cj0XVM4POxjN7EFQqmKCMFWhIRKw7Suz7s1TTh0mjfX+hv0V66LTf9L2RkRDjsJ6YCisUunecY2Rgp7nNpN5zSWpcoL0EuFuX0UTifEO86LikLu5l5DXcGg1eM1xB+/6BuzdxqnLxoM6C1wevIw0hngwZ6tFrC33yeztDcuLH3BfAV7sMplPGD3lUhs7A7n0z6XSmq0L/tmZnsEYdqlXFBHNp7n1P6hDxdWP8cdsbHv46KhzRf2Fvlu) |
| 62 | + |
| 63 | +## Nope, still doesn't work |
| 64 | + |
| 65 | +If you're still getting an empty object, then perhaps your `FormData` object really is empty. |
| 66 | + |
| 67 | +If you're creating the `FormData` object by passing in a `<form>`, the first thing to try is appending a hardcoded dummy field, just to check that it shows up in the logging. For example: |
| 68 | + |
| 69 | +```js |
| 70 | +const formData = new FormData(myForm.value) |
| 71 | + |
| 72 | +formData.append('dummy', 'foo') |
| 73 | + |
| 74 | +console.log(Object.fromEntries(formData.entries())) |
| 75 | +``` |
| 76 | + |
| 77 | +In the example above, `myForm` is a [template ref](https://vuejs.org/guide/essentials/template-refs.html), so we pass `myForm.value` to the `FormData` constructor. |
| 78 | + |
| 79 | +- [Playground example](https://play.vuejs.org/#eNp9Ustu2zAQ/JUFL5IBQz60p8Ap+nKL9tAUaY68KPRKlcMXyKXjwPC/d0k6ahIE0UncmR3OzvIoPnnf7ROKC7GOKkyeICIlD7q346UUFKX4IO1kvAsER0gRb9B43RNe4wAnGIIz0LBCI620ytlIYB6+uWDg8gW7bSrQLDJ1SFbR5CyogIxn4GtPfbuAo7QAVWk4V1nL4j3MpCrU7XudsKjBTO1679Fu22abjHloltAMztUrq6rT2Gk3tle3O1TU5QE2lsKEsZ018FxYLLjxJO16VcPhKPhA55n4BLDOTRBw4LSqLSlgVaHbRMQTflR6UneMPx+1JAv8fXeFvap0Lq5XT64QS94CGx+msdtFZ3lVJSGWc8ZPGsOVz0Hypi5qdhnrtXb3P0uNQsLlY139RXX3Sn0XD7kmxe+AEcMepZgx6sOIVOHNn1944P8ZNG6bNLPfAK+RQ0/ZY6V9TnbLtp/witsf5ZFNdryJmwOhjY9DZaOZeSp8Kfi1fXlj9P9233XvSx/vUJz+AcVD/ZA=) |
| 80 | + |
| 81 | +If we can see the dummy field in the console logging then we know the problem isn't with the logging. The `FormData` really was empty. |
| 82 | + |
| 83 | +The `FormData` constructor will usually throw an error if you pass in an invalid value, but it allows `undefined` to be passed. Try console logging the value you're passing to `FormData`, to confirm it's an `HTMLFormElement` and not `undefined`: |
| 84 | + |
| 85 | +```js |
| 86 | +console.log(myForm.value) // Check it isn't undefined |
| 87 | +const formData = new FormData(myForm.value) |
| 88 | +``` |
| 89 | + |
| 90 | +Let's assume the form passed to the constructor isn't `undefined`, what else could be going wrong? |
| 91 | + |
| 92 | +If possible, use the **Elements** tab in your browser developer tools to inspect the `<form>` (or inspect the logged value in the console). Check that it actually contains valid fields, such as `<input>` elements. Then: |
| 93 | + |
| 94 | +1. Ensure each element has a `name`. This is used as the key to populate the `FormData`. An `id` isn't sufficient, it needs a `name`. |
| 95 | +2. Check the element isn't disabled. Disabled elements will be ignored. |
| 96 | +3. For an `<input>` with `type="checkbox"`, it will only be included if the checkbox is checked. |
| 97 | + |
| 98 | +Reference: <https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData> |
0 commit comments