Consider this Deno Fresh form component. What will be the value of formData.get('username') inside the handler after submission?
import { Handlers } from "https://deno.land/x/fresh@1.1.5/server.ts"; export const handler: Handlers = { async POST(req) { const formData = await req.formData(); return new Response(formData.get('username')); } }; export default function Page() { return ( <form method="POST"> <input name="username" defaultValue="denoUser" /> <button type="submit">Send</button> </form> ); }
Think about what formData.get returns when the input has a value.
The form sends the input named username with value denoUser. The formData.get('username') returns that string.
Given this Deno Fresh handler that updates a counter from a form submission, what will be the response text after submitting with count=5?
import { Handlers } from "https://deno.land/x/fresh@1.1.5/server.ts"; let counter = 0; export const handler: Handlers = { async POST(req) { const formData = await req.formData(); const count = Number(formData.get('count')); counter += count; return new Response(`Counter is now ${counter}`); } };
Remember that formData.get returns a string and you convert it to a number.
The formData.get('count') returns the string '5'. Converting it to Number gives 5. Adding 5 to the initial counter 0 results in 5.
Which of the following code snippets correctly reads form data and updates a user object in a Deno Fresh POST handler?
Check which method returns a promise and how to correctly access form data.
Option A correctly awaits req.formData() which returns a promise, then gets the 'name' field. Option A misses await, C uses json() which is incorrect for form data, D misses parentheses on formData call.
Given this handler code, why does it throw a TypeError: formData.get is not a function?
export const handler = {
async POST(req) {
const formData = req.formData();
const email = formData.get('email');
return new Response(email);
}
};Check if the formData variable is a promise or the actual data object.
req.formData() returns a promise. Without await, formData is a promise object, which does not have get() method, causing the TypeError.
Which statement best describes how form mutations are processed in Deno Fresh server-side handlers?
Think about how HTTP requests and form data work in server handlers.
Deno Fresh handlers receive the HTTP request, parse form data asynchronously, and can mutate server state before sending a response. Client-side binding is manual, and form data is not JSON by default.
