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

时间:2019-03-06 20:52:43

标签: javascript ecmascript-6 es6-promise next.js contentful

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

有人在我做错事情的地方可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

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

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