简体   繁体   English

为什么在尝试循环下一个 js 页面中的数据(使用 getStaticProps)时我得到未定义?

[英]Why am I getting undefined when attempting to loop over data in next js page (using getStaticProps)?

Having difficulty working with outputting the page meta data in next just when I run:在我运行时难以在下一个输出页面元数据:

npm run build

I get the following error:我收到以下错误:

Error occurred prerendering page "/blog/[...slug]". Read more: https://err.sh/next.js/prerender-error
TypeError: Cannot read property 'map' of undefined

This is the page I'm having an issue with:这是我遇到问题的页面:

//pages/blog/[...slug].js
import Head from 'next/head'
import Layout, { siteTitle } from '../../components/layout'
import axios from 'axios';



export default function About({ meta, title }) {

  return (
    <Layout home>
      <Head>
        <title>{title} | {siteTitle}</title>
        {meta.map(meta_value => {
          return (
            <meta name={meta_value.name || meta_value.property} content={meta_value.content} />
          );
        })}
      </Head>
    </Layout>
  )
}


export async function getStaticProps(context) {
  
  const res = await axios.get(`https://example.com/api/posts?url=/${context.params.slug.join('/')}`);
  const post = await res.data;
  
  return {
    props: {
      title: post.title,
      meta: post.meta
    },
  }

}


export async function getStaticPaths() {
  
  const res = await axios.get('https://example.com/api/posts?categories=3');
  const posts = await res.data;

  const slugs = posts.map(post => post.permalink.replace(/^\/|\/$/g, '').split("/") );
  const paths = slugs.map(slug => ({params: { slug } } ));

  return {
    paths,
    fallback: true,
  }


}

Sample response from api:来自 api 的示例响应:

  {

    "title" : "test",

    "meta": [
          
            {
                "property": "og:locale",
                "content": "en_GB"
            },
            {
                "property": "og:type",
                "content": "article"
            },
            {
                "property": "og:title",
                "content": "Test Post"
            },
            {
                "property": "og:description",
                "content": "testing"
            },
            {
                "property": "og:url",
                "content": "https://example.com/blog/2021/01/test/"
            },
            {
                "property": "og:site_name",
                "content": "Test Site"
            },
            {
                "property": "article:published_time",
                "content": "2021-01-30T13:32:26+00:00"
            },
            {
                "property": "article:modified_time",
                "content": "2021-01-30T13:58:00+00:00"
            },
            {
                "name": "twitter:card",
                "content": "summary_large_image"
            },
            {
                "name": "twitter:label1",
                "content": "Estimated reading time"
            },
            {
                "name": "twitter:data1",
                "content": "1 minute"
            }
        ]
      }

"getStaticProps" only runs at build time, which means that the context parameter won't contain "params" which comes from the url. “getStaticProps”仅在构建时运行,这意味着上下文参数不会包含来自 url 的“params”。 Which i'm guessing will result in an response with undefined data.我猜这将导致未定义数据的响应。

Try to replace GetStaticProps with getServerSideProps , which prerenders the page with new data on each request instead of at build time.尝试将 GetStaticProps 替换为getServerSideProps ,它会在每个请求而不是在构建时使用新数据预呈现页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jQuery将HTML字符串放入首页时,为什么会变得未定义? - Why am I getting undefined when putting a string of html to the main page using jQuery? 为什么在Next.js中调用getStaticProps会生成JSON文件? - Why is a JSON file generated when calling getStaticProps in Next.js? Next.js getStaticProps 将数据作为对象传递 - Next.js getStaticProps passing data as object 为什么我在运行 changeword.js 时会得到 undefined 这个词? - Why am I getting the word undefined when I run changeword.js? 为什么在此for循环中得到未定义的offset:124? - Why am I getting an undefined offset:124 in this for loop? 为什么我在此 for 循环中无法读取未定义的属性“startsWith”? - Why am I getting Cannot read property 'startsWith' of undefined in this for loop? 为什么我在控制器中的变量未定义,但它在带有数据绑定的HTML页面中正确显示 - Why am I getting undefined for my variable in the controller but it displays correctly in the HTML page with data binding 为什么用for循环功能会得到未定义的结果? - why I am getting undefined result with for loop function? 为什么我得到“TypeError:无法读取未定义的属性&#39;恢复&#39;当使用sinon.js删除mongoose时 - Why Am I getting “TypeError: Cannot read property 'restore' of undefined” when stubbing mongoose with sinon.js 为什么我在 Discord.js 中尝试获得公会时得到未定义的信息? - Why am I getting an undefined when trying to get a Guild in Discord.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM