Practice
1. What is the main difference between a
content collection and a data collection in Astro?easy
Solution
Step 1: Understand content collections
Content collections are designed to hold content that visitors see, like blog posts or articles.Step 2: Understand data collections
Data collections store internal or site-related information, such as settings or configuration data.Final Answer:
Content collections hold visitor-facing content, while data collections store internal information. -> Option AQuick Check:
Content = visitor content, Data = internal info [OK]
Hint: Remember: content = visitor content, data = internal info [OK]
Common Mistakes:
- Thinking content collections are only for images
- Assuming data collections are visible to visitors
- Believing schemas are only for data collections
2. Which of the following is the correct way to define a schema field for a
content collection in Astro?easy
Solution
Step 1: Recall Astro schema syntax
Astro schemas use lowercase types likestringto define fields.Step 2: Check each option
"title: string" uses "title: string" which matches the correct syntax. Others use incorrect capitalization or assignment.Final Answer:
"title: string" -> Option DQuick Check:
Schema fields use lowercase types like string [OK]
Hint: Use lowercase type names in schema fields [OK]
Common Mistakes:
- Using uppercase types like String
- Using equals sign instead of colon
- Using undefined types like Text
3. Given this Astro content collection schema snippet:
And this content file:
What will be the value of
{ title: 'string', published: 'boolean' }And this content file:
--- title: "Hello World" published: true ---
What will be the value of
published when queried?medium
Solution
Step 1: Understand schema field type
The schema definespublishedas a boolean, so the value should be a true boolean, not a string.Step 2: Check content file value
The content file setspublished: true(boolean true), matching the schema type.Final Answer:
true -> Option AQuick Check:
Boolean field returns boolean value true [OK]
Hint: Match schema types exactly for correct data values [OK]
Common Mistakes:
- Confusing boolean true with string "true"
- Expecting numeric 1 for true
- Assuming missing or null value
4. You wrote this schema for a data collection:
But when querying,
{ siteName: 'string', maxUsers: 'number' }But when querying,
maxUsers is always null. What is the likely error?medium
Solution
Step 1: Check schema type usage
Astro supports 'number' as a valid type, so 'number' is correct.Step 2: Consider data file consistency
IfmaxUsersis null, likely the data files have a different field name or missing value.Final Answer:
The field namemaxUsersis misspelled in the data files. -> Option BQuick Check:
Field name mismatch causes null values [OK]
Hint: Check field names match exactly in schema and data [OK]
Common Mistakes:
- Assuming 'number' is invalid type
- Thinking data collections disallow numbers
- Using uppercase type names incorrectly
5. You want to create a content collection for blog posts with fields:
title (string), tags (array of strings), and published (boolean). Which schema definition is correct?hard
Solution
Step 1: Understand array field syntax in Astro schemas
Astro uses an object withtype: 'array'anditemsto define arrays.Step 2: Evaluate each option
{ title: 'string', tags: { type: 'array', items: 'string' }, published: 'boolean' } correctly uses{ type: 'array', items: 'string' }. Others use invalid or unsupported syntax.Final Answer:
{ title: 'string', tags: { type: 'array', items: 'string' }, published: 'boolean' } -> Option CQuick Check:
Arrays use type: 'array' with items field [OK]
Hint: Use object with type 'array' and items for arrays [OK]
Common Mistakes:
- Using 'string[]' as type string
- Using array literal syntax in schema
- Assuming 'array<string>' is valid
