Forms let users send data to your app. Mutations change data based on what users send.
Forms and mutations in Deno
import { serve } from "https://deno.land/std@0.203.0/http/server.ts"; serve(async (req) => { if (req.method === "POST") { const formData = await req.formData(); const name = formData.get("name"); // perform mutation with name return new Response(`Hello, ${name}! Your data was saved.`); } return new Response( ` <form method="POST"> <input name="name" placeholder="Your name" required /> <button type="submit">Send</button> </form> `, { headers: { "content-type": "text/html; charset=utf-8" } } ); });
Use req.formData() to get form data from a POST request.
Mutations happen when you change or save data based on form input.
const formData = await req.formData(); const email = formData.get("email");
if (req.method === "POST") { // handle form submission }
<form method="POST"> <input name="message" /> <button type="submit">Send</button> </form>
This Deno server shows a form. When you type a message and submit, it sends the message to the server. The server reads the message and replies with a confirmation. This is a simple example of a form and mutation.
import { serve } from "https://deno.land/std@0.203.0/http/server.ts"; serve(async (req) => { if (req.method === "POST") { const formData = await req.formData(); const message = formData.get("message"); // Simulate saving the message (mutation) return new Response(`Message received: ${message}`); } return new Response( ` <form method="POST"> <label for="message">Enter message:</label> <input id="message" name="message" required /> <button type="submit">Submit</button> </form> `, { headers: { "content-type": "text/html; charset=utf-8" } } ); });
Always use method="POST" for forms that change data.
Use formData.get() to safely get each form field.
Remember to set the response content type to text/html when returning HTML.
Forms collect user input to send to your app.
Mutations happen when you change or save data from forms.
Deno's req.formData() helps read form data easily.
Practice
req.formData() method do in a Deno server context?Solution
Step 1: Understand the purpose of
This method is designed to extract form data sent by the client in a POST request.req.formData()Step 2: Identify what the method returns
It returns a FormData object containing key-value pairs from the submitted form.Final Answer:
It reads and returns the submitted form data from the request. -> Option AQuick Check:
Form data reading = It reads and returns the submitted form data from the request. [OK]
- Thinking formData() sends data to client
- Assuming formData() validates data automatically
- Believing formData() clears data from request
Solution
Step 1: Recall the method signature for formData()
In Deno,req.formData()returns a Promise, so it must be awaited.Step 2: Identify the correct syntax
The correct syntax usesawait req.formData();to get the form data asynchronously.Final Answer:
const data = await req.formData(); -> Option AQuick Check:
Async form data retrieval = const data = await req.formData(); [OK]
- Forgetting to use await with formData()
- Using a non-existent method getFormData()
- Calling formData() without async context
const formData = await req.formData();
const name = formData.get('name');
return new Response(`Hello, ${name}!`);What will be the response if the form submitted has
name=Alice?Solution
Step 1: Extract the 'name' value from formData
The code usesformData.get('name')which returns the string 'Alice' from the submitted form.Step 2: Construct the response string
The response template string inserts 'Alice' correctly, resulting in 'Hello, Alice!'.Final Answer:
Hello, Alice! -> Option DQuick Check:
formData.get('name') = 'Alice' [OK]
- Assuming get() returns undefined instead of null
- Not using await with formData() causing errors
- Misreading template string interpolation
const formData = req.formData();
const email = formData.get('email');
return new Response(email);Solution
Step 1: Check how formData() is called
The methodreq.formData()returns a Promise and must be awaited.Step 2: Identify the error caused by missing await
Without await, formData is a Promise, so calling get() on it causes a runtime error.Final Answer:
Missing await before req.formData() causing a Promise error. -> Option CQuick Check:
Always await formData() to get FormData object [OK]
- Calling get() on a Promise instead of FormData
- Assuming formData() is synchronous
- Ignoring async/await syntax rules
username and age. Which code correctly reads the form and updates a user object, ensuring age is stored as a number?Solution
Step 1: Await formData() to get the form data object
Usingawait req.formData()correctly retrieves the form data asynchronously.Step 2: Convert the 'age' string to a number properly
UsingNumber(formData.get('age'))converts the age string to a number safely.Step 3: Check the username assignment
Assigningusername: formData.get('username')correctly stores the username string.Final Answer:
const formData = await req.formData(); const user = { username: formData.get('username'), age: Number(formData.get('age')) }; -> Option BQuick Check:
Await + Number conversion = const formData = await req.formData(); const user = { username: formData.get('username'), age: Number(formData.get('age')) }; [OK]
- Not awaiting formData() causing errors
- Storing age as string without conversion
- Parsing wrong field for age
Ready to prove it?
Pass the test and this pattern moves unknown
