Complete the code to define a form action that handles form submission.
export const actions = {
default: async ({ request }) => {
const formData = await request.formData();
const name = formData.get([1]);
return { success: true, name };
}
};The form action extracts the "name" field from the submitted form data using formData.get("name").
Complete the code to return a mutation result from the form action.
export const actions = {
default: async ({ request }) => {
const formData = await request.formData();
const message = formData.get("message");
// Simulate saving message
return { [1]: true, message };
}
};error or loading instead of success.The form action returns an object with success: true to indicate the mutation succeeded.
Fix the error in the form action that prevents mutation handling.
export const actions = {
default: async ({ request }) => {
const formData = await request.[1]();
const email = formData.get("email");
return { success: true, email };
}
};formdata() causes a runtime error.formData.get directly on request.The correct method to get form data is request.formData() with capital D and no extra parentheses inside.
Fill both blanks to correctly handle a form mutation and return a message.
export const actions = {
default: async ({ request }) => {
const formData = await request.[1]();
const comment = formData.get([2]);
return { success: true, comment };
}
};formdata() or wrong field name.The method to get form data is formData() and the field name is "comment".
Fill all three blanks to handle a form mutation that updates user data.
export const actions = {
default: async ({ request }) => {
const formData = await request.[1]();
const username = formData.get([2]);
const age = Number(formData.get([3]));
// pretend to save username and age
return { success: true, username, age };
}
};formdata() or wrong field names causes errors.The method is formData(), and the form fields are "username" and "age".