Next.js getInitialProps 上下文查询为空
Next.js getInitialProps context query is empty
我正在关注关于 Next.js 的基本 tutorial。
他们提供了这个代码:
Post.getInitialProps = async function(context) {
const { id } = context.query;
const res = await fetch(`https://api.tvmaze.com/shows/${id}`);
const show = await res.json();
console.log(`Fetched show: ${show.name}`);
return { show };
};
我已经复制粘贴了。但是 context.query 是一个空对象,因此,我的 api 调用失败了。
这是我重定向到该动态页面的代码 (pages/p/[id].js)
<Layout>
<h1>Batman TV Shows</h1>
<ul>
{props.shows.map(show => (
<li key={show.id}>
<Link href="/p/[id]" as={`/p/${show.id}`}>
<a>{show.name}</a>
</Link>
</li>
))}
</ul>
</Layout>
这是一个错误,还是我遗漏了什么?
好的,答案是从 v8.0.4 更新到 v9.0.1 或更新版本。教程here写的很清楚
我正在关注关于 Next.js 的基本 tutorial。
他们提供了这个代码:
Post.getInitialProps = async function(context) {
const { id } = context.query;
const res = await fetch(`https://api.tvmaze.com/shows/${id}`);
const show = await res.json();
console.log(`Fetched show: ${show.name}`);
return { show };
};
我已经复制粘贴了。但是 context.query 是一个空对象,因此,我的 api 调用失败了。 这是我重定向到该动态页面的代码 (pages/p/[id].js)
<Layout>
<h1>Batman TV Shows</h1>
<ul>
{props.shows.map(show => (
<li key={show.id}>
<Link href="/p/[id]" as={`/p/${show.id}`}>
<a>{show.name}</a>
</Link>
</li>
))}
</ul>
</Layout>
这是一个错误,还是我遗漏了什么?
好的,答案是从 v8.0.4 更新到 v9.0.1 或更新版本。教程here写的很清楚