我有下一个应用程序。我需要实现逻辑时,路由是不匹配的段塞页,然后显示404页错误。
接下来,就我所知的显示404页而言,我需要返回值为true.链接的true对象。
所以问题是,当我从{ notFound: true }返回getServerSideProps时,为什么会出现这个错误?
错误:从
getServerSideProps返回了其他密钥。用于组件的属性必须嵌套在props键下,例如: 返回{ props:{ title:'My Title',content:‘.’}} 需要移动的键: notFound。
代码:
export const getServerSideProps: GetServerSideProps = async ({ params, req }) => {
const { slug } = params;
// first request
const data = await (await fetch(`${process.env.NEXT_PUBLIC_API_HOST}/${slug}`)).json();
// second request
const user = await fetch(`${process.env.NEXT_PUBLIC_API_HOST}`, {
method: "GET",
headers: {
'Authorization': 'Bearer ' + "jwt",
'Content-Type': 'application/json',
},
});
const userInfo = await user.json();
if ( !slug || data.statusCode === 404 ) return { notFound: true }
return {
props: {
title: "something",
// my props in here
},
}
}只有当我用url写了一些东西,并且有意识地将我的弹格页面从正确更改为不正确时,它才会产生错误。例如,从localhost/page/1到localhost/page/blablabla。
在本例中,当我将路由更改为错误时,如果情况是(if ( !slug || data.statusCode === 404 ) ).Next版本9.5.2,则使用此方法。
发布于 2021-04-01 21:25:13
https://stackoverflow.com/questions/66911218
复制相似问题