I have a page where I want to list all content from one collection and all data that each item references from another collection. I am having trouble working with collection references within a loop.
I have the two following schemas:
const peopleCollection = defineCollection({
type: "content",
schema: z.object({
name: z.string(),
avatar: z.string(),
})
});
const projectCollection = defineCollection({
type: "content",
schema: z.object({
title: z.string(),
teamMembers: z.array(reference("people")),
})
});
On my index.astro page, I want to list all projects, displaying their titles and associated team member names.
// Set in my frontmatter
const projects = await getCollection("projects");
// In the markup
<div>
{ projects.map((project) => (
<article>
<p>{ project.data.title }</p>
<!-- How can I make the following work? -->
{ project.data.teamMembers.map((person) => (
<p>{ person.data.name }</p>
))}
</article>
))}
</div>