我知道这可能是一个很容易回答的问题,但几个小时后我无法弄清楚。我正在尝试映射这个 JSON,但它一直说“props.shows.map”不是一个函数,但同样适用于另一位数据。我试图从数据中获取“id”和“name”之类的东西。
该映射适用于此链接:https ://api.tvmaze.com/search/shows?q=\%22Terrance%20House\%22
这是不起作用/似乎无法从以下位置获取信息的 JSON:https ://api.tvmaze.com/shows/34275/episodes
import Layout from '../components/MyLayout'
import fetch from 'isomorphic-unfetch'
import Link from 'next/link'
const ShowLink = ({ show }) => (
<li key={show.id}>
<Link as={`/episode/${show.id}`} href={`/episodes/id=${show.id}`}>
<a>{show.name}</a>
</Link>
<style jsx>{`
li {
list-style: none;
margin: 5px 0;
}
a {
text-decoration: none;
color: blue;
font-family: "Arial";
}
a:hover {
opacity: 0.6;
}
`}</style>
</li>
)
/*
<h1>{props.show.name}</h1>
<p>{props.show.summary.replace(/<[/]?p>/g, '')}</p>
<img src={props.show.image.medium}/>
*/
const Post = (props) => (
<Layout>
<h1>Terrace House Episodes</h1>
<ul>
{props.shows.map(({show}) => (
<ShowLink key={show} value={show} />
))}
</ul>
</Layout>
)
Post.getInitialProps = async function () {
//const { id } = context.query
//const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
//const show = await res.json()
//const res = await fetch(`http://api.tvmaze.com/seasons/34275/episodes`);
const res = await fetch('https://api.tvmaze.com/shows/34275/episodes');
const data = await res.json()
return {
shows: data
}
}
export default Post