Practice
1. What does
Astro.fetchContent do in rendering collection content?easy
Solution
Step 1: Understand the purpose of Astro.fetchContent
This function is used to get data from a collection, so you can use it in your page.Step 2: Identify what happens with the data
The fetched data is then used to show each item on the page, not to style, delete, or create collections.Final Answer:
It fetches data from a collection to display on the page. -> Option CQuick Check:
Astro.fetchContent fetches data [OK]
Hint: Remember: fetch means get data, not change it [OK]
Common Mistakes:
- Thinking fetchContent styles or deletes data
- Confusing fetching with creating collections
2. Which of the following is the correct syntax to loop through a collection named
posts in Astro to render each item?easy
Solution
Step 1: Identify the correct method to transform arrays in Astro
Astro uses JavaScript array methods;.map()returns a new array of elements for rendering.Step 2: Check other options for correctness
forEachdoes not return an array, so it won't render correctly.loopandeachare not valid JavaScript array methods.Final Answer:
{posts.map(post => <div>{post.title}</div>)} -> Option DQuick Check:
Use .map() to render collections [OK]
Hint: Use .map() to create elements from arrays [OK]
Common Mistakes:
- Using forEach which returns undefined
- Using non-existent methods like loop or each
3. Given the collection
products with items [{name: 'Pen'}, {name: 'Book'}], what will this Astro code output?{products.map(p => `${p.name}
`).join('')}medium
Solution
Step 1: Understand the map function output
Each product's name is wrapped in <p> tags, creating an array of strings: ['Pen
', 'Book
'].Step 2: Understand the join('') method
Joining the array with empty string combines the strings into one: 'Pen
Book
'.Final Answer:
<p>Pen</p><p>Book</p> -> Option AQuick Check:
map + join creates combined HTML string [OK]
Hint: map returns array; join('') combines to string [OK]
Common Mistakes:
- Expecting array output instead of joined string
- Confusing object output with string output
4. You wrote this code to render a collection but get an error:
What is the likely problem?
const items = Astro.fetchContent('items');
return items.map(item => <li>{item.name}</li>);What is the likely problem?
medium
Solution
Step 1: Check how Astro.fetchContent works
This function returns a Promise, so you must await it to get the data array.Step 2: Identify the error cause
Without await, items is a Promise object, so calling map on it causes an error.Final Answer:
Astro.fetchContent returns a Promise, but you didn't await it. -> Option BQuick Check:
Always await async fetchContent calls [OK]
Hint: Always await fetchContent to get data array [OK]
Common Mistakes:
- Not awaiting async fetchContent
- Confusing map and forEach usage
- Assuming fetchContent returns data directly
5. You want to render a list of blog posts but only show posts with a
published flag set to true. Which Astro code correctly fetches and renders only published posts from the blogs collection?hard
Solution
Step 1: Fetch the collection data asynchronously
UseawaitwithAstro.fetchContentto get the posts array.Step 2: Filter posts with published flag true before rendering
Usefilter(post => post.published)to keep only published posts, then map to render.Final Answer:
Fetch with await, filter published, then map to render. -> Option AQuick Check:
Filter before map for conditional rendering [OK]
Hint: Filter first, then map to render filtered items [OK]
Common Mistakes:
- Not awaiting fetchContent before filtering
- Filtering on Promise instead of data array
- Returning undefined for unfiltered items
