【发布时间】:2019-04-14 13:47:26
【问题描述】:
我正在我的本地主机上设置一个 NextJS 应用程序,并按照网站上的精彩教程进行操作,但是在插入我的本地 API 时我遇到了问题。
更改 JS 代码时出现错误。错误是
TypeError: Cannot read property 'id' of undefined
(anonymous function)
/Users/mattcohen/hello-
next/.next/server/static/development/pages/index.js:1611:17
这是我对 api 的声明:
Index.getInitialProps = async function() {
const res = await fetch('http://localhost:8888/api/mods')
const data = await res.json()
console.log(`Show data fetched. Count: ${data.length}`)
return {
mods: data.mods.map(entry => entry.show)
}
}
export default Index
这是我的页面布局代码
import Layout from '../components/MyLayout.js'
import Link from 'next/link'
import fetch from 'isomorphic-unfetch'
const Index = (props) => (
<Layout>
<h1>Batman TV Shows</h1>
<ul>
{props.mods.map(mod => (
<li key={mod.id}>
<Link as={`/p/${show.id}`} href={`/post?id=${mod.id}`}>
<a>{mod.brand_name}</a>
</Link>
</li>
))}
</ul>
有什么想法吗?
【问题讨论】: