| Level Navigation: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19⚡ | 20 |
Goal: Update your form handler to insert new meals into the database.
User Story: As a user, I want my form submission to save the meal to the database so that it persists and can be retrieved later.
Update your handleAddMeal function to include the database insertion logic.
handleAddMeal function to include the insert logicawait supabase.from("potluck_meals").insert(newMeal) to insert the new mealNeed help with database insertion? Check out these snippets:
async function handleAddMeal(event){
event.preventDefault()
console.log("handle add meal submitted")
const mealName = event.target.elements.mealName.value
const guestName = event.target.elements.guestName.value
const serves = event.target.elements.serves.value
const kindOfDish = event.target.elements.kindOfDish.value
const newMeal = {
meal_name: mealName,
guest_name: guestName,
serves: parseInt(serves),
kind_of_dish: kindOfDish
}
console.log(newMeal)
// Insert the new meal
await supabase.from("potluck_meals").insert(newMeal)
}
Understanding CRUD operations:
INSERT - Adding new records to the databaseSELECT - Retrieving data from the databaseUPDATE - Modifying existing recordsDELETE - Removing records from the databaseDatabase insertion explained:
await supabase.from("potluck_meals").insert(newMeal)
supabase.from("table_name"): Specifies which table to insert into.insert(data): Inserts the provided data as a new rowawait: Waits for the insertion to complete before continuingExploring network requests:
To see what’s actually being sent to Supabase, open your browser’s Developer Tools:
F12 or right-click → “Inspect”newMeal object)What you’ll see:
https://your-project.supabase.co/rest/v1/potluck_mealsPOST (for inserting data)Data persistence:
Error handling considerations:
In production apps, you’d want to handle potential errors:
const { data, error } = await supabase.from("potluck_meals").insert(newMeal)
if (error) {
console.error('Insert failed:', error);
// Show user-friendly error message
} else {
console.log('Meal added successfully:', data);
// Update UI to show success
}
Database constraints:
📺 Learn More: