Next Js TypeError:无法读取undefined的属性'xxx'

问题描述 投票:1回答:1

当我运行“下一次导出”时,我收到以下错误。我的构建很好,但导出失败。我一直在关注很多教程,但找不到解决方案。我是否正确定义了我的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  

错误:

TypeError:无法读取未定义的属性'someEntryAsProp'

任何人都可以帮助我,我做错了吗?

javascript ecmascript-6 es6-promise next.js contentful
1个回答
0
投票

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

const Index = ({someEntryAsProp}) => (...)
© www.soinside.com 2019 - 2024. All rights reserved.