Skip to content

Unable to Access formData with createServerFn() #3137

Closed

Description

@matheuspergoli

Which project does this relate to?

Start

Describe the bug

I am using the example provided in the documentation for createServerFn(), but I am unable to access formData as expected. Despite following the implementation as shown, the function doesn't seem to work as intended.

I took the code example for using createServerFn() with formData directly from the documentation and placed it in /app/routes/index.tsx

If there's any additional setup or a specific requirement not mentioned in the documentation, please let me know.

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-i9fhqwar?file=app%2Froutes%2Findex.tsx

Steps to Reproduce the Bug or Issue

  1. Go to /
  2. Submit with some values
  3. Check the terminal for the errors

Expected behavior

I expect to be able to access formData normally within createServerFn() as demonstrated in the documentation example.

Screenshots or Videos

Image
Image

Platform

  • OS: Windows
  • Browser: MS Edge
  • Version: 131.0.2903.112

Additional context

No response

Activity

  1. added
    start
    on Jan 10, 2025
  2. SeanCassiere commented on Jan 10, 2025

    @SeanCassiere
    Member

    I'm not sure if we ever had this or not but clearly this needs to be fixed.

    Currently, submitting FormData to the server function using the .url works as expected, however, submitting it using the functions caller doesn't.

    i.e.

    // works
    <form method='POST' action={serverFn.url}>
    // ...
    </form>
    
    
    // fails
    <form
      onSubmit={(e) => {
        e.preventDefault();
        const input = new FormData(e.currentTarget)
        serverFn({ data: input })
      }}
    >
    //..
    </form>

    The serializer/transformer needs to be enhanced to support serializing the entries in an instantiated FormData instance into something that can be sent over the wire and safely deserialize it on the server.

  3. SeanCassiere commented on Jan 10, 2025

    @SeanCassiere
    Member

    Need to investigate further why the multipart form data end-to-end example works, but this does not.

    const handleSubmit = (e: React.MouseEvent<HTMLButtonElement>) => {
    e.preventDefault()
    if (!formRef.current) {
    return
    }
    const formData = new FormData(formRef.current)
    multipartFormDataServerFn({ data: formData }).then(setMultipartResult)
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    start

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions