How do we get the fields without having to do this individually?
const name = (formData.get("name") ?? "") as string;
Can we map or iterate it?
CODE
export const action: ActionFunction = async ({
request,
}: ActionFunctionArgs) => {
const formData = await request.formData();
const name = (formData.get("name") ?? "") as string;
const website_url = (formData.get("website_url") ?? "") as string;
const user_id = (formData.get("user_id") ?? "") as string;
try {
// call api here
return null;
} catch (error) {
throw json({ error: "Error" }, { status: 500 });
}
};
2
Answers
You can use
Object.fromEntries()
static method and pass your formData object to it it will convert standarad js object.you iterate through formData using forEach
TS palyground