[英]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.