I want to filter the posts by tag.The tag can use {post.frontmatter.tag} to get.When I try to add that into filter but it don't work.
there are error msg:TypeError: Cannot read properties of undefined (reading 'includes')
I get the meta by the frontmatter such as {post.frontmatter.tag}.It is working.There are code:
<Container>
<Heading as="h3" fontSize={20} mb={4}>
Blog
</Heading>
<Section delay={0.1}>
<div className="posts">
{posts.filter(post => post.frontmatter.tag.includes('a')).map(filteredposts => (
<li>{filteredposts}</li>
))}
</div>
</Section>
</Container>
</layout>
)
}
export async function getStaticProps() {
// Get files from the posts dir
const files = fs.readdirSync(path.join('posts'))
// Get slug and frontmatter from posts
const posts = files.map(filename => {
// Create slug
const slug = filename.replace('.md', '')
// Get frontmatter
const markdownWithMeta = fs.readFileSync(
path.join('posts', filename),
'utf-8'
)
const { data: frontmatter } = matter(markdownWithMeta)
return {
slug,
frontmatter
}
})
return {
props: {
posts: posts.sort(sortByDate)
}
}
}