Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing referenced collection items is not working as documented #13052

Closed
1 task
MoritzLost opened this issue Jan 23, 2025 · 2 comments
Closed
1 task

Accessing referenced collection items is not working as documented #13052

MoritzLost opened this issue Jan 23, 2025 · 2 comments
Labels
needs triage Issue needs to be triaged

Comments

@MoritzLost
Copy link

MoritzLost commented Jan 23, 2025

Astro Info

Astro                    v5.1.8
Node                     v20.18.1
System                   macOS (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @vite-pwa/astro-integration

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I want to reference a collection item in a property from another collection. I have defined the property crops using the reference helper, as documented in Defining collection references:

schema: z.object({
    crop: reference('crops'),
}),

Then in my content items (JSON files using the glob loader in this case), I reference an item from the crops collection by ID:

{
    "crop": "grapevine"
}

Now I want to load the grapevine collection item in a template:

const { region } = Astro.props;
const crop = await getEntry('crops', region.data.crop)!;

This is giving me type warnings, and it doesn't return the collection item:

Image

Typehint tells me that region.data.crop is an object with keys collection and id. Therefore, I tried to access the ID, this works correctly and gets rid of the type warnings:

const { region } = Astro.props;
const crop = await getEntry('crops', region.data.crop.id)!;

Is the reference helper supposed to transform the reference into an object like this?

I'm not sure if this is a bug or if the documentation is just outdated. The documentation on Accessing referenced data only shows using the property key, not accessing a nested id property.

What's the expected result?

Not sure if the documentation needs to be updated, or the behaviour I'm seeing is a bug.

Link to Minimal Reproducible Example

Not applicable.

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 23, 2025
@ascorbic
Copy link
Contributor

Hi. You should change it to:

const { region } = Astro.props;
const crop = await getEntry(region.data.crop)!;

You don't include the collection name, because it's in the reference object.

@MoritzLost
Copy link
Author

@ascorbic Ah thanks, I missed that!

It would be useful to mention in the docs that the reference() helper transforms the property, I don't think this is documented anywhere. I've opened an issue on that: withastro/docs#10774

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants