const Post = () => {
const router = useRouter();
const slug = router.query.postslug;
const currentPost = PostData.find((post) => post.slug === slug);
return (
currentPost.content
)
}
export default Post;
Server Error
TypeError: Cannot read properties of undefined (reading ‘content’)
Below is the array of objects
const PostData = [
{
id:1,
slug:...,
content:...
}
]
>Solution :
The problem is that content is undefined in context of currentPost.
Its undefined because PostData doesnt match the slug.
a quick guard should help the solution
{ currentPost && currentPost.content}