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

Updated docs to reflect that getEntry returns undefined #10688

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/content/docs/en/guides/content-collections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,29 @@ Once queried, you can render Markdown and MDX entries to HTML using the [`render
import { getEntry, render } from 'astro:content';

const entry = await getEntry('blog', 'post-1');
if(!entry){
// Handle Error
}
const { Content, headings } = await render(entry);
---
<p>Published on: {entry.data.published.toDateString()}</p>
<Content />
```

`getEntry()` returns either an entry object or `undefined`.

1. Conditional Check (Recommended)
```ts
if (!entry) {
//Handle Error
}
```
2. Non-Null Assertion Operator
```ts
// Use the ! operator to assert the value is non-null
const { Content, headings, remarkPluginFrontmatter } = await render(entry!);
```

#### Passing content as props

A component can also pass an entire collection entry as a prop.
Expand Down
21 changes: 21 additions & 0 deletions src/content/docs/en/reference/modules/astro-content.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,31 @@ A function to compile a given entry for rendering. This returns the following pr
---
import { getEntry, render } from 'astro:content';
const entry = await getEntry('blog', 'entry-1');


// Handle undefined return
if (!entry) {
//Handle Error
}
const { Content, headings, remarkPluginFrontmatter } = await render(entry);
---
```

`getEntry()` returns either an entry object or `undefined`.

1. Conditional Check (Recommended)
```ts
if (!entry) {
//Handle Error
}
```
2. Non-Null Assertion Operator
```ts
// Use the ! operator to assert the value is non-null
const { Content, headings, remarkPluginFrontmatter } = await render(entry!);
```


[See the `Content Collection` guide](/en/guides/content-collections/#rendering-body-content) for example usage.

## `astro:content` types
Expand Down
Loading