[英]props are returned as string instead of object
I'm currently implementing user authentication on my website, and I'm trying to restrict access to the login and signup pages if the user is logged in. To do so, I am implementing this getServerSideProps
to redirect the user if they're logged in:我目前正在我的网站上实施用户身份验证,并且如果用户登录,我正在尝试限制对登录和注册页面的访问。为此,我正在实施此
getServerSideProps
以在用户登录时重定向用户在:
export async function getServerSideProps(context) {
if (context.req.cookies.user) {
return {
redirect: {
destination: '/',
permanent: true,
},
}
}
return {}
}
If I return an empty object ( {}
), next gives me this error:如果我返回一个空的 object (
{}
),接下来会给我这个错误:
Reason: Props must be returned as a plain object from getServerSideProps: `{ props: { ... } }` (received: `[object Array]`).
If instead of returning {}
, I return如果不是返回
{}
,而是返回
{
props: {}
}
the code works all right.该代码工作正常。 Does anyone know why this is happening?
有谁知道为什么会这样?
I'm using a custom _app.jsx
file.我正在使用自定义
_app.jsx
文件。
In the documentation, it's clearly mentioned that the props argument is optional:在文档中,明确提到 props 参数是可选的:
This is actually intended.这实际上是有意的。 If you were using TypeScript,
return {}
would have thrown you error as GetServerSidePropsResult
is defined like this here :如果您使用的是 TypeScript,
return {}
会抛出错误,因为GetServerSidePropsResult
在此处定义如下:
export type GetServerSidePropsResult<P> =
| { props: P | Promise<P> }
| { redirect: Redirect }
| { notFound: true }
It means that any one of props
, redirect
, notFound
needs to be present in the returned object.这意味着任何一个
props
、 redirect
、 notFound
都需要出现在返回的 object 中。 Perhaps you can create an issue at their GitHub (and maybe a PR) to fix the documentation.也许您可以在他们的GitHub (也可能是 PR)上创建一个问题来修复文档。
As mentioned by @tromgy, it was just the documentation that is unclear.正如@tromgy 所提到的,只是文档不清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.