简体   繁体   中英

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?

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

Can anyone please help me where I am doing wrong?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM