| Level Navigation: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19⚡ | 20 |
Goal: Update the fetch function to actually retrieve data from Supabase.
User Story: As a developer, I want to fetch data from my database so that I can see what meals are stored.
Update the handleFetchMeals function to fetch data from Supabase and log it to console.
handleFetchMeals function to use await supabase.from("potluck_meals").select()setMeals(data)Need help with data fetching? Check out these snippets:
async function handleFetchMeals() {
console.log("Fetching meals...")
const result = await supabase.from("potluck_meals").select()
const data = result.data
console.log("Fetched data:", data);
setMeals(data);
}
What is async/await?
async: Marks a function as asynchronous, meaning it can use await and will return a Promiseawait: Pauses execution until a Promise resolves, then returns the resolved valueUnderstanding Supabase queries:
const result = await supabase.from("potluck_meals").select()
supabase.from("table_name"): Specifies which table to query.select(): Retrieves all columns from the table (like SELECT * in SQL)result: Contains both data and error propertiesresult.data: The actual data returned from the databaseresult.error: Any error that occurred during the queryError handling pattern:
Always check for errors when working with databases:
if (result.error) {
console.error('Database error:', result.error);
return; // Stop execution if there's an error
}
📺 Learn More: