简体   繁体   English

Next Js TypeError:无法读取未定义的属性“ xxx”

[英]Next Js TypeError: Cannot read property 'xxx' of undefined

I get the below error when I run "next export". 运行“下一个导出”时出现以下错误。 My build is fine but the export fails. 我的构建很好,但是导出失败。 I have been following a lot of tutorials but couldn't find a solution. 我一直在关注很多教程,但是找不到解决方案。 Have I defined my someEntryAsProp correctly? someEntryAsProp正确定义了someEntryAsProp

import Layout from '../components/myLayout.js'
import Link from 'next/link'
import {createClient} from '../helpers/getcontent';
import { type } from 'os';


function getPosts(){
  return [
    { id:'hello-nextjs' , title:'Hello Next.js' },
    { id:'learn-nextjs' , title:'Learn Next.js is awesome' },
    { id:'deploy-nextjs' , title:'Deploy apps with Zeit' },
  ]
}
const PostLink = ({ post }) => (
  <li>
    <Link as={`/p/${post.id}`} href={`/post?title=${post.title}`}>
      <a>{post.title}</a>
    </Link>
  </li>
)

const Index = () => (

  <Layout>
    <h1>My Blog</h1>
    <p>{someEntryAsProp.fields.title}</p>
    <ul>
    { getPosts().map((post) => (
       <PostLink key={post.id} post={post}/>
    ))}
    </ul>
  </Layout>
  );

  Index.getInitialProps = async () => {

    console.log('> Starting import',);
   const client = createClient();
     const entries = await client.getEntries({
       // some query
      content_type:type,
       include:2
     })

   console.log(entries.items[0])
     console.log('> Content gotten and written for',)

    return { someEntryAsProp: entries.items[0] };
    //return {};
  };

  export default Index  

Error: 错误:

TypeError: Cannot read property 'someEntryAsProp' of undefined TypeError:无法读取未定义的属性“ someEntryAsProp”

Can anyone please help me where I am doing wrong? 谁能帮我在哪里做错了?

您需要将prop作为参数传递给页面组件:

const Index = ({someEntryAsProp}) => (...)

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

相关问题 TypeError:无法读取未定义jQuery的属性“ xxx” - TypeError: Cannot read property 'xxx' of undefined jQuery 下一个 js:TypeError:无法读取未定义的属性“地图” - Next js: TypeError: Cannot read property 'map' of undefined JS - 通过键访问 object 值,但出现消息 [TypeError: Cannot read property 'XXX' of undefined] - JS - access the object value by key but occur the message [TypeError: Cannot read property 'XXX' of undefined] React / Webpack / Django-未捕获的TypeError:无法读取未定义的属性“ XXX” - React/Webpack/Django - Uncaught TypeError: Cannot read property 'XXX' of undefined 无法读取未定义的属性“ xxx” - Cannot read property `xxx` of undefined JS TypeError:无法读取未定义的属性“ map” - JS TypeError: Cannot read property 'map' of undefined TypeError:无法读取react js中未定义的属性“then” - TypeError: Cannot read property 'then' of undefined in react js 类型错误:无法设置未定义的属性“xxx” - TypeError: Cannot set property 'xxx' of undefined Next.js、Firebase、未处理的运行时错误 - TypeError:无法读取未定义的属性“initializeApp” - Next.js, Firebase, Unhandled Runtime Error - TypeError: Cannot read property 'initializeApp' of undefined TypeError:无法读取未定义的属性'refs' - Next.js App &amp; React - TypeError: Cannot read property 'refs' of undefined - Next.js App & React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM