在 apollo 上下文中使用 getSession 时出现 CLIENT_FETCH_ERROR

问题描述 投票:0回答:2

我正在构建一个在前端使用 Next-Auth 并在后端使用 Apollo 服务器的应用程序。

这是我的后端代码:

   const server = new ApolloServer({
    schema,
    csrfPrevention: true,
    cache: 'bounded',
    context: async ({ req, res }) => {
        const session = await getSession({ req })
        return { session }

    },
    plugins: [
        ApolloServerPluginDrainHttpServer({ httpServer }),
        ApolloServerPluginLandingPageLocalDefault({ embed: true }),
    ],
});

我收到这个错误:

[next-auth][error][CLIENT_FETCH_ERROR]
https://next-auth.js.org/errors#client_fetch_error fetch is not defined {
  error: {
    message: 'fetch is not defined',
    stack: 'ReferenceError: fetch is not defined\n' +
      '    at _callee$ (/Users/adi/Desktop/chat/backend/node_modules/next-auth/client/_utils.js:52:13)\n' +
      '    at tryCatch (/Users/adi/Desktop/chat/backend/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:72:17)\n' +
      '    at Generator._invoke (/Users/adi/Desktop/chat/backend/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:55:24)\n' +
      '    at Generator.next (/Users/adi/Desktop/chat/backend/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:97:21)\n' +
      '    at asyncGeneratorStep (/Users/adi/Desktop/chat/backend/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)\n' +
      '    at _next (/Users/adi/Desktop/chat/backend/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)\n' +
      '    at /Users/adi/Desktop/chat/backend/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7\n' +
      '    at new Promise (<anonymous>)\n' +
      '    at /Users/adi/Desktop/chat/backend/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12\n' +
      '    at _fetchData (/Users/adi/Desktop/chat/backend/node_modules/next-auth/client/_utils.js:88:21)',
    name: 'ReferenceError'
  },
  url: 'http://localhost:3000/api/auth/session',
  message: 'fetch is not defined'
}

我的 .env 中有 NEXTAUTH_URL= http://localhost:3000。 我错过了什么?

session next.js graphql apollo-server next-auth
2个回答
0
投票

升级您的节点版本,这将解决该问题


0
投票

我遇到的唯一可行的解决方案是调用

http://localhost:3000/api/auth/session

分别端点和提取会话。 您可以在 this URL 上找到完整版本。向下滚动到最后一个,@gaganbiswas

© www.soinside.com 2019 - 2024. All rights reserved.