I have an object including web content:
const postContent = {
post1: {
title: 'Post 1',
content: 'Content 1'
},
post2: {
title: 'Post 2',
content: 'Content 2'
}
}
And a dynamic [post].js:
import { postContent } from "../../lib";
import { useRouter } from "next/router";
const postWeb = () => {
const router = useRouter();
const { post } = router.query;
const postGet = postContent[post]
console.log(postGet)
return (
<div>
{postGet.title}
</div>
);
}
export default postWeb;
I use [post] to get the object from postContent like postContent.post1 (depend on URL like /post/post1 or /post/post2)
I use console.log(postGet) and receive an object:
{title: 'Post 1', content: 'Content 1'}
. But when I use postGet.title, it will be undefined and receive an error:
TypeError: Cannot read properties of undefined (reading 'title')
This error happened while generating the page. Any console logs will be displayed in the terminal window
I don't know how to fix this, I'm figured out how to use dynamic route with content in Nextjs. Can anyone help me with these or can give me tutorials about these?
Thank you.