【发布时间】:2021-03-27 11:21:33
【问题描述】:
在 components/featured-posts.js 中的 next.js 中使用 getStaticProps 时,我得到 Cannot read property 'map' of undefined 但如果我直接在pages/index.js 页面显示结果,有人知道它为什么这样做吗?
export async function getStaticProps(){
const data = await fetch('http://localhost/newblog/newblogapi/posts')
const posts=await data.json();
return {
props: { posts }, // will be passed to the page component as props
}
}
const FeaturedPosts = ({posts}) => {
console.log(posts)
return (
<div className="album py-5 bg-light">
<div className="container mt-5">
{ posts.map(post=>(
<div key={post.id}>
<h3>{post.title}</h3>
</div>
))}
</div>
</div>
);
}
export default FeaturedPosts;
【问题讨论】: