任何人都可以帮助我处理这些动态内容(nextJ)

发布于 2025-01-27 02:38:45 字数 1131 浏览 2 评论 0原文

我有一个包括Web内容的对象:

const postContent = {
    post1: {
        title: 'Post 1',
        content: 'Content 1'
    },
    post2: {
        title: 'Post 2',
        content: 'Content 2'
    }
}

和一个动态[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;

我使用[post]从postcontent等postcontcontent.post1(依赖 /post /post /post /post1或 /post /post /post2)获取对象。 我使用console.log(Postget)并接收一个对象: {title:'post 1',content:'content 1'}。但是,当我使用postget.title时,它将是未定义的并收到错误:

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

我不知道如何解决此问题,我发现如何在NextJ中使用Dynamic Route。谁能帮助我解决这些问题,或者可以给我一些有关这些的教程?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2025-02-03 02:38:45

最初渲染中的问题

TypeError: Cannot read properties of undefined (reading 'title')

,您正在访问最初不存在的对象的属性。

解决方案

考虑添加默认值或短路

  return (
        <div>
            {postGet && postGet.title}
        </div>
    );

Problem

TypeError: Cannot read properties of undefined (reading 'title')

In your initial rendering, you are accessing a property of an object that is initially doesn't exist.

Solution

Consider adding default value or short circuiting

  return (
        <div>
            {postGet && postGet.title}
        </div>
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文