【发布时间】:2021-12-20 00:12:14
【问题描述】:
我正在开发一个全栈博客站点项目,在该项目中我为后端创建了一个 API,它工作正常,我正在从中获取帖子,但是当我尝试使用 map 对其进行迭代时,它会抛出错误
Server Error TypeError: Cannot read properties of undefined (reading 'map'),我正在使用 NextJS 作为我的前端。
代码
const Blogs = ({ blogs }) => {
return (
<div className='all-blogs'>
{blogs.map((blog) => (
<div className='single-blog' key={blog._id}>
<h1>{blog.title}</h1>
<p>{blog.text}</p>
</div>
))}
</div>
)
}
export async function getStaticProps() {
const res = await fetch(
'https://ed-blog-api.herokuapp.com/api/posts'
)
const blogs = await res.json()
return {
props: {
blogs,
},
}
}
我正在获取的端点:https://ed-blog-api.herokuapp.com/api/posts
[
【问题讨论】:
标签: javascript reactjs api next.js