Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to fetch data from an API in Astro frontmatter.
Astro
---
const response = await fetch([1]);
const data = await response.json();
--- Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of the URL string.
Calling a function that is not defined.
✗ Incorrect
In Astro frontmatter, you use fetch with the API URL as a string to get data.
2fill in blank
mediumComplete the code to handle the JSON response correctly in Astro frontmatter.
Astro
--- const response = await fetch('https://api.example.com/data'); const data = await response.[1](); ---
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
text() which returns raw text.Using
blob() or xml() which are not for JSON.✗ Incorrect
The json() method parses the response body as JSON.
3fill in blank
hardFix the error in the Astro frontmatter fetch code to await the fetch call.
Astro
--- const response = [1]('https://api.example.com/data'); const data = await response.json(); ---
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not awaiting the fetch call causes errors when calling
json().Using
then() without proper async handling.✗ Incorrect
You must await the fetch call to get the response before calling json().
4fill in blank
hardFill both blanks to create a frontmatter fetch that gets JSON and extracts the 'items' property.
Astro
--- const response = await fetch([1]); const data = await response.[2](); const items = data.items; ---
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
text() instead of json().Using the wrong URL string.
✗ Incorrect
Use the correct API URL and json() to parse the response.
5fill in blank
hardFill all three blanks to fetch data, parse JSON, and map item names in Astro frontmatter.
Astro
--- const response = await fetch([1]); const data = await response.[2](); const names = data.items.map(item => item.[3]); ---
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong URL for fetching.
Not parsing JSON before mapping.
Accessing a wrong property instead of 'name'.
✗ Incorrect
Fetch from the correct URL, parse JSON, then map the 'name' property from each item.