Astro shows “Invalid use of getDataEntryById or getEntryBySlug function” when code calls either of these older content collection helpers. They were replaced by a single function, and the message names it: “Use getEntry instead.” The change is mechanical: swap the function name and pass the entry’s id.
Key Takeaways
getEntryBySlug('blog', slug)becomesgetEntry('blog', id).getDataEntryById('authors', id)becomesgetEntry('authors', id).- Ids replaced slugs; for most files they’re the same string.
getEntry()returnsundefinedwhen nothing matches, so check the result.
Find every use
grep -rn "getEntryBySlug\|getDataEntryById" src/
Replace them
- import { getEntryBySlug } from 'astro:content';
- const post = await getEntryBySlug('blog', Astro.params.slug);
+ import { getEntry } from 'astro:content';
+ const post = await getEntry('blog', Astro.params.slug!);
- import { getDataEntryById } from 'astro:content';
- const author = await getDataEntryById('authors', 'komal');
+ import { getEntry } from 'astro:content';
+ const author = await getEntry('authors', 'komal');
getEntry() also accepts a reference object, which is how you resolve reference() fields:
const author = await getEntry(post.data.author);
Guard against missing entries
const post = await getEntry('blog', Astro.params.slug!);
if (!post) return new Response(null, { status: 404 });
Without the guard, rendering fails with Attempted to render an undefined content collection entry.
Other changes in the same upgrade
If these helpers are in your code, the project probably predates the Content Layer API. Expect to also:
- move
src/content/config.tstosrc/content.config.ts(Legacy content config file found), - add a
loaderto each collection (Content collection is missing a loader), - replace
entry.slugwithentry.idandentry.render()withrender(entry).
Run npx astro sync and npx astro check afterwards; type errors point at anything left over.
The Astro content collections guide includes a step-by-step migration from older collections, and the astro:content reference documents getEntry().
FAQ
Is the id the same as the old slug?
For most files, yes: both come from the file path. Projects that customised slugs should compare built URLs before and after.
Can getEntry() fetch several entries at once?
Use getEntries() with an array of references.