question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve FormData logging (or documentation)

See original GitHub issue

Describe the problem

I’m always frustrated when working with await event.request.formData() because it is not easily log-able.

export async function post(event) {
    const body = await event.request.formData()
    console.log('body', body)
    console.log('body', body.entries())
    console.log('body', body.keys())
    console.log('body', body.values())
    ...

All those simple measures during development to see if form data arrives and what values are contained do not work with current SvelteKit:

body FormData {}
body Object [Generator] {}
body Object [Generator] {}
body Object [Generator] {}

Especially the fact that a simple console.log(body) yields no sense-making output is very irritating to me. As a SvelteKit newbie it took me an hour to understand that it’s not a problem with the data transfer but only with the way I want to display the data.

Describe the proposed solution

const body = await event.request.formData();
console.log(body);

should show body content, formatting doesn’t really matter.

[ [ "name": "Rich Harris" ], [ "hobbies", "svelte" ], [ "hobbies": "journalism" ] ]

Alternatives considered

Add an example to the endpoint body parsing documentation.

const body = await event.request.formData();
const entries = [...body.entries()];
// [ [ "name": "Rich Harris" ], [ "hobbies", "svelte" ], [ "hobbies": "journalism" ] ]

Importance

would make my life easier

Additional Information

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
Rich-Harriscommented, Mar 4, 2022

Yeah, we definitely don’t want to be monkey-patching or mucking about with prototypes. The MDN FormData documentation also isn’t much help in this regard though.

Logging is pretty straightforward once you know how…

const body = await event.request.formData();

// to get everything
console.log(...body); // ["name", "Rich Harris"] ["hobbies", "svelte"], ["hobbies", "journalism"]

// if you know you don't have duplicates
console.log(Object.fromEntries(body)); // { name: "Rich Harris", hobbies: "journalism" }

…and the same thing applies to lots of similarly-shaped objects (Map, URLSearchParams, etc).

I’m always a little torn in situations like this. If Svelte/SvelteKit docs need to show how to use various web APIs (including things like Request and Response) then they’ll quickly get bloated. At the same time, many developers will encounter those APIs for the first time while using Svelte, and it can be hard to find relevant/high quality documentation elsewhere. There’s no right answer.

0reactions
bluepuma77commented, Mar 12, 2022

Thanks for clarification, will close issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using FormData Objects - Web APIs | MDN
The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest. It is primarily intended for use in sending...
Read more >
How to inspect FormData? - javascript
Longer answer. Others suggest logging each entry of fd.entries() , but the console.log can also take multiple arguments console ...
Read more >
FormData
FormData objects are used to capture HTML form and submit it using fetch or another network method. We can either create new FormData(form)...
Read more >
FormData For JavaScript. How form data is sent, and files too!
You often learn a lot when reading API documentation, and in this case I ... log => data type = object FormData {...
Read more >
How to Inspect the JavaScript FormData Instance?
We create the formData object with the FormData constructor. ... In the for-of loop, we destructure the key-value pairs and log them with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found