I'm pretty sure I'm using Asyncs and Awaits wrong. I don't understand why. Can anyone shed any light on why this doesn't work?
The error
Error: Error serializing `.posts[0]` returned from `getStaticProps` in "/notion".
Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value.
The code for the page's NextJS static props function:
export async function getStaticProps() {
const postsResponse = await fetch(
`https://api.notion.com/v1/databases/${process.env.BLOG_DB}/query`, {
headers: {
Authorization: `Bearer ${process.env.NOTION_TOKEN}`,
"Notion-Version": `${process.env.NOTION_VERSION}`,
},
method: "POST",
})
const postsJSON = await postsResponse.json()
const posts = postsJSON.results.map((post) => {
const blocksResponse = await fetch(
`https://api.notion.com/v1/blocks/${post.id}/children`, {
headers: {
Authorization: `Bearer ${process.env.NOTION_TOKEN}`,
"Notion-Version": `${process.env.NOTION_VERSION}`,
},
method: "GET",
})
const blocksJSON = await blocksResponse.json()
const blocks = blocksJSON.results.map((block) => {
return {
id: block.id,
type: block.type,
text: block[block.type].text[0].plain_text,
link: block[block.type].text[0].href,
}
})
return {
id: post.id,
title: post.properties.Title.title[0].plain_text,
tags: post.properties.Tags.multi_select.map(tag => tag.name),
blocks: [],
}
});
return {
props: { posts }
};
}
End goal is to fetch children blocks for each blog-post and return it all as an array.
[
{
post_id: "id of blog post",
post_title: "title of blog post",
children: [...] // the children of the item
},
{
post_id: "id of blog post",
post_title: "title of blog post",
children: [...] // the children of the item
}
]