next.js 在apollo上下文中使用getSession时发生CLIENT_FETCH_ERROR

s2j5cfk0  于 2023-04-20  发布在  其他
关注(0)|答案(1)|浏览(121)

我在前端使用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。我缺少什么?

1bqhqjot

1bqhqjot1#

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

相关问题