如何使用 Prisma 解决 NextAuth 中的“adapter_error_getUserByAccount”问题

Sam*_*ung 6 authentication prisma next-auth

我正在尝试使用 Next.js + NextAuth.js + Prisma 在我的项目中实现身份验证流程。我正在使用 GitHub Provider。它在我的本地计算机上运行得很好,我可以登录并重定向回仪表板。我认为这可能是数据库问题,所以我使用我的生产数据库 URL 进行了测试,但在本地环境中仍然工作正常。仅在生产模式下才会出现错误。

以下是我在 Vercel 中收到的错误消息。

[GET] /api/auth/callback/github?code=e19191fb050f6a1708e8&state=lV7j49LEjSWE9GHMBWjQ-6WGr62yqziPqhkWURnGBWQ
15:55:11:41
2022-05-16T06:55:11.782Z    4b29fadc-0c2a-4f7a-9140-fbbb45d159f2    ERROR   [next-auth][error][adapter_error_getUserByAccount] 
https://next-auth.js.org/errors#adapter_error_getuserbyaccount Cannot read property 'findUnique' of undefined {
  message: "Cannot read property 'findUnique' of undefined",
  stack: "TypeError: Cannot read property 'findUnique' of undefined\n" +
    '    at getUserByAccount (/var/task/node_modules/@next-auth/prisma-adapter/dist/index.js:11:45)\n' +
    '    at _callee2$ (/var/task/node_modules/next-auth/core/errors.js:315:29)\n' +
    '    at tryCatch (/var/task/node_modules/regenerator-runtime/runtime.js:63:40)\n' +
    '    at Generator.invoke [as _invoke] (/var/task/node_modules/regenerator-runtime/runtime.js:294:22)\n' +
    '    at Generator.next (/var/task/node_modules/regenerator-runtime/runtime.js:119:21)\n' +
    '    at asyncGeneratorStep (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)\n' +
    '    at _next (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)\n' +
    '    at /var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7\n' +
    '    at new Promise (<anonymous>)\n' +
    '    at /var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12',
  name: 'TypeError'
}
2022-05-16T06:55:11.783Z    4b29fadc-0c2a-4f7a-9140-fbbb45d159f2    ERROR   [next-auth][error][OAUTH_CALLBACK_HANDLER_ERROR] 
https://next-auth.js.org/errors#oauth_callback_handler_error Cannot read property 'findUnique' of undefined TypeError: Cannot read property 'findUnique' of undefined
    at getUserByAccount (/var/task/node_modules/@next-auth/prisma-adapter/dist/index.js:11:45)
    at _callee2$ (/var/task/node_modules/next-auth/core/errors.js:315:29)
    at tryCatch (/var/task/node_modules/regenerator-runtime/runtime.js:63:40)
    at Generator.invoke [as _invoke] (/var/task/node_modules/regenerator-runtime/runtime.js:294:22)
    at Generator.next (/var/task/node_modules/regenerator-runtime/runtime.js:119:21)
    at asyncGeneratorStep (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at /var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at /var/task/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12 {
  name: 'GetUserByAccountError',
  code: undefined
}
Run Code Online (Sandbox Code Playgroud)

我已阅读 GitHub 问题和讨论上的所有相关主题,但仍然找不到解决此问题的任何方法。

小智 3

您需要使用“schema.prisma”创建具有正确字段的 prisma 文件

https://next-auth.js.org/adapters/prisma

然后 :

npx prisma db push
npx prisma generate
Run Code Online (Sandbox Code Playgroud)