类型错误:无法读取未定义的属性(读取“站点”)

发布于 2025-01-11 01:56:25 字数 3114 浏览 1 评论 0原文

您好,我遇到了这个错误,我尝试了很多方法,但不知道如何解决。 它可能是一个库还是我需要做其他事情?

这里有错误和我找到它的代码。

> error - components/Layout.tsx (31:4) @ Layout
>     TypeError: Cannot read properties of undefined (reading 'site')
>       29 | 
>       30 |   const { name, url, title, description, socialPreview } =
>     > 31 |     publicRuntimeConfig.site;
>          |    ^
>       32 | 
>       33 |   const meta = {
>       34 |     name,

Layout.tsx(它是一个组件,我在我正在开发的另一个网站中具有相同的结构,但我在该网站中没有错误)

import { setConfig } from 'next/config'
import { publicRuntimeConfig } from '../next.config';
import getConfig from 'next/config';
import Head from 'next/head';
import { useRouter } from 'next/router';

setConfig({ publicRuntimeConfig })

type LayoutProps = {
  title?: string;
  description?: string;
  date?: string;
  socialPreview?: string;
  children: React.ReactNode;
};

const Layout = ({ children, ...customMeta }: LayoutProps) => {
  const router = useRouter();
  const { asPath } = router;

  const { name, url, title, description, socialPreview } =
    publicRuntimeConfig.site;

  const meta = {
    name,
    url,
    title,
    description,
    socialPreview,
    ...customMeta,
  };

  return (
    <>
      <Head>
        <link rel="icon" href="/images/fev.svg" key="favicon" />
        <link rel="canonical" href={`${url}${asPath}`} key="canonical" />

        {/* Twitter */}
        <meta
          name="twitter:card"
          content="summary_large_image"
          key="twitter_card"
        />
        <meta name="twitter:title" content={meta.title} key="twitter_title" />
        <meta
          name="twitter:description"
          content={meta.description}
          key="twitter_description"
        />
        <meta
          name="twitter:image"
          content={`${url}${socialPreview}`}
          key="twitter_image"
        />

        {/* Open Graph */}
        <meta property="og:url" content={`${url}${asPath}`} key="og_url" />
        <meta property="og:site_name" content={meta.name} key="og_site_name" />
        <meta property="og:title" content={meta.title} key="og_title" />
        <meta
          property="og:description"
          content={meta.description}
          key="og_description"
        />
        <meta
          property="og:image"
          content={`${url}${socialPreview}`}
          key="og_image"
        />
        <meta property="og:image:width" content={`1200`} key="og_image_width" />
        <meta
          property="og:image:height"
          content={`630`}
          key="og_image_height"
        />

        <meta name="description" content={meta.description} key="description" />
        {meta.date && (
          <meta property="article:published_time" content={meta.date} />
        )}
        <title key="title">{meta.title}</title>
      </Head>
      <main>{children}</main>
    </>
  );
};

export default Layout;

Hello, I got this error, I tried a lot of things but I don't know how to solve it.
Could it be a library or do I need to do something else?

Here you have the error and the code where I found it.

> error - components/Layout.tsx (31:4) @ Layout
>     TypeError: Cannot read properties of undefined (reading 'site')
>       29 | 
>       30 |   const { name, url, title, description, socialPreview } =
>     > 31 |     publicRuntimeConfig.site;
>          |    ^
>       32 | 
>       33 |   const meta = {
>       34 |     name,

Layout.tsx (It is a component, I have the same structure in another website I'm working on, but I have no errors in that one)

import { setConfig } from 'next/config'
import { publicRuntimeConfig } from '../next.config';
import getConfig from 'next/config';
import Head from 'next/head';
import { useRouter } from 'next/router';

setConfig({ publicRuntimeConfig })

type LayoutProps = {
  title?: string;
  description?: string;
  date?: string;
  socialPreview?: string;
  children: React.ReactNode;
};

const Layout = ({ children, ...customMeta }: LayoutProps) => {
  const router = useRouter();
  const { asPath } = router;

  const { name, url, title, description, socialPreview } =
    publicRuntimeConfig.site;

  const meta = {
    name,
    url,
    title,
    description,
    socialPreview,
    ...customMeta,
  };

  return (
    <>
      <Head>
        <link rel="icon" href="/images/fev.svg" key="favicon" />
        <link rel="canonical" href={`${url}${asPath}`} key="canonical" />

        {/* Twitter */}
        <meta
          name="twitter:card"
          content="summary_large_image"
          key="twitter_card"
        />
        <meta name="twitter:title" content={meta.title} key="twitter_title" />
        <meta
          name="twitter:description"
          content={meta.description}
          key="twitter_description"
        />
        <meta
          name="twitter:image"
          content={`${url}${socialPreview}`}
          key="twitter_image"
        />

        {/* Open Graph */}
        <meta property="og:url" content={`${url}${asPath}`} key="og_url" />
        <meta property="og:site_name" content={meta.name} key="og_site_name" />
        <meta property="og:title" content={meta.title} key="og_title" />
        <meta
          property="og:description"
          content={meta.description}
          key="og_description"
        />
        <meta
          property="og:image"
          content={`${url}${socialPreview}`}
          key="og_image"
        />
        <meta property="og:image:width" content={`1200`} key="og_image_width" />
        <meta
          property="og:image:height"
          content={`630`}
          key="og_image_height"
        />

        <meta name="description" content={meta.description} key="description" />
        {meta.date && (
          <meta property="article:published_time" content={meta.date} />
        )}
        <title key="title">{meta.title}</title>
      </Head>
      <main>{children}</main>
    </>
  );
};

export default Layout;

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文