小编Juj*_*ujV的帖子

将 GetServerSideProps 与 Next-Auth 结合使用:TypeError:无法解构“req.query”的属性“nextauth”,因为它未定义

我在 next.js 中使用 getServerSideProps 函数和 Next-Auth ,但出现 TypeError:

TypeError: Cannot destructure property 'nextauth' of 'req.query' as it is undefined.
Run Code Online (Sandbox Code Playgroud)

当我使用控制台检查时,它确实返回未定义。

我一直在关注 NextAuth.js 的官方文档:https ://next-auth.js.org/configuration/nextjs#getserversession

我的功能:

export const getServerSideProps = async (context: { req: NextApiRequest; res: NextApiResponse<any>; }) => {

  const session = await getServerSession(context.req, context.res, authOptions)

  if (!session) {
    return {
      redirect: {
        destination: '/',
        permanent: false
      }
    }
  }

  return {
    props: {
      session,
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我做:

const {req: query} = context
console.log(query == undefined)
Run Code Online (Sandbox Code Playgroud)

控制台返回 false,但 …

typescript next.js getserversideprops next-auth

5
推荐指数
1
解决办法
2735
查看次数