Remix: Unable to get formData from the routes

clock icon

asked 2 months ago

message

1 Answers

eye

1 Views

Working with a remix project which is trying to receive data from the form. When I receive the form data from my React component, I am unable to retrieve any fields from formData.get.

Here is my code in the route,

export async function action(data: any) {
    const { request } = data;

    console.log(`request`);
    console.log(request);

    const formData = await request.formData();

    console.log(`formData`);
    console.log(formData);
    
    const noteData: Note = {
        title: formData.get("title")?.toString() ?? "",
        description: formData.get("description")?.toString() ?? "",
    };
    const note = createNote(noteData);
    console.log(`New note created => Note - ${JSON.stringify(note)}`);
}

Above I have added some log statements as well. Noticing that formData is returning an empty object.

formData
formData {}

How can this be fixed so that I can retrieve the form data?

EDIT: Adding the React Component below which uses the action,

    export default function Note() {
        return (
            <form method="post" action="/notes" id="quote-form">
                <p>
                    <label htmlFor="title">Title</label>
                    <input type="text" id="title" name="title" required />
                </p>
                <p>
                    <label htmlFor="content">Content</label>
                    <textarea id="content" name="content" required />
                </p>
                <div>
                    <button> Add Note </button>
                </div>
            </form>
        );

}

The route is shown above with the function name action.

1 Answers

request.formData returns a Promise, await it.

Example:

export async function action(data: any) {
  const { request } = data;

  ...

  const formData = await request.formData();

  ...
}

Additionally, you cannot simply console log a FormData object to get the field data, but instead you'll need to access its field data.

Examples:

 

const formData = new FormData();
formData.set("foo", 123);
formData.set("bar", "barbar");
formData.append("foo", 456);

console.log("formData", formData); // no field data
console.log("get foo", formData.get("foo"));
console.log("getAll foo", formData.getAll("foo"));
console.log("get entries", ...formData.entries());

 

The form you are submitting doesn't have a "description" field, it has title and content fields.

export default function Note() {
  return (
    <form method="post" action="/notes" id="quote-form">
      <p>
        <label htmlFor="title">Title</label>
        <input type="text" id="title" name="title" required />
      </p>
      <p>
        <label htmlFor="content">Content</label>
        <textarea id="content" name="content" required />
      </p>
      <div>
        <button type="submit">Add Note</button>
      </div>
    </form>
  );
}
export async function action(data: any) {
  const { request } = data;

  const formData = await request.formData();

  console.log("formData", ...formData.entries());
    
  const noteData: Note = {
    title: formData.get("title")?.toString() ?? "",
    description: formData.get("content")?.toString() ?? "",
  };

  const note = createNote(noteData);
  console.log(`New note created => Note - ${JSON.stringify(note)}`);
}
 

Top Questions