Next-Auth集成Prisma报错:TypeError: 无法读取undefined的'user'属性
Next-Auth + Prisma适配器调用getUserByEmail时报错Prisma客户端undefined
问题详情
已按文档配置Next-Auth使用EmailProvider和Prisma适配器,Prisma能正常连接数据库,但在登录流程中调用getUserByEmail时,Prisma客户端突然变为undefined,抛出错误:TypeError: Cannot read properties of undefined (reading 'user')。
配置代码
authOptions代码
const prisma = globalThis.prisma || new PrismaClient(); globalThis.prisma = prisma; export const authOptions = { // Configure one or more authentication providers adapter: PrismaAdapter(prisma), providers: [ EmailProvider({ server: process.env.EMAIL_SERVER, from: process.env.EMAIL_FROM }) }
.env配置
DATABASE_URL=postgresql://dbname:password@localhost:5432/dbname?schema=public EMAIL_SERVER=smtp://admin@myemail.ca:password@mail.privateemail.com:465 EMAIL_FROM=from@email.com
报错相关代码片段
next-auth/core/lib/email/getUserFromEmail代码
async function getAdapterUserFromEmail({ email, adapter }) { const { getUserByEmail } = adapter; const adapterUser = email ? await getUserByEmail(email) : null; // 此处调用getUserByEmail,Email为登录时传入的字符串 if (adapterUser) return adapterUser; return { id: email, email, emailVerified: null }; }
@next-auth/prisma-adapter/dist/index.js代码
function PrismaAdapter(p) { return { createUser: (data) => p.user.create({ data }), getUser: (id) => p.user.findUnique({ where: { id } }), getUserByEmail: (email) => getUserByEmail: (email) => { p.user.findUnique({ where: { email } }) // p为undefined }, } }
报错栈追踪
error - TypeError: Cannot read properties of undefined (reading 'user') at getUserByEmail (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/@next-auth/prisma-adapter/dist/index.js:8:38) at _callee2$ (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next-auth/core/errors.js:365:29) at tryCatch (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:44:17) at Generator.<anonymous> (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:125:22) at Generator.next (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:69:21) at asyncGeneratorStep (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24) at _next (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9) at /Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7) at new Promise (<anonymous>) at /Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12) at getAdapterUserFromEmail (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next-auth/core/lib/email/getUserFromEmail.js:15:37) at Object.signin (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next-auth/core/routes/signin.js:77:54) at AuthHandler (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next-auth/core/index.js:253:39) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async NextAuthHandler (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next-auth/next/index.js:23:19) at async /Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next-auth/next/index.js:59:32) at async Object.apiResolver (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next/dist/server/api-utils/node.js:363:9) at async DevServer.runApi (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next/dist/server/next-server.js:487:9) at async Object.fn (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next/dist/server/next-server.js:749:37) at async Router.execute (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next/dist/server/router.js:253:36) at async DevServer.run (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next/dist/server/base-server.js:384:29) at async DevServer.run (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next/dist/server/dev/next-dev-server.js:741:20) at async DevServer.handleRequest (/Users/ryan/Documents/Projects/animated_section_builder/app-platform/node_modules/next/dist/server/base-server.js:322:20) { name: 'GetUserByEmailError', code: undefined, page: '/api/auth/[
解决方案
1. 修复authOptions的语法错误
你的authOptions代码中providers数组和外层对象未闭合,补上缺失的括号:
export const authOptions = { adapter: PrismaAdapter(prisma), providers: [ EmailProvider({ server: process.env.EMAIL_SERVER, from: process.env.EMAIL_FROM }) ], // 补上数组闭合括号 // 可添加其他配置,比如session策略等 } // 补上对象闭合括号
2. 重新安装依赖,修复适配器代码异常
从提供的适配器代码片段看,getUserByEmail存在重复定义的语法错误,这大概率是依赖安装或打包时的异常。执行以下操作:
# 删除依赖和锁文件 rm -rf node_modules package-lock.json # 重新安装依赖 npm install
3. 规范Prisma客户端全局初始化
将Prisma客户端的初始化单独抽离到文件(比如prisma/client.js),避免全局变量的异步问题:
import { PrismaClient } from '@prisma/client' const prisma = global.prisma || new PrismaClient() if (process.env.NODE_ENV !== 'production') global.prisma = prisma export default prisma
然后在authOptions所在文件中导入:
import prisma from '@/prisma/client'
4. 确保依赖版本兼容
检查next-auth、@next-auth/prisma-adapter和prisma的版本是否匹配,建议使用经过验证的兼容版本,比如在package.json中锁定:
{ "next-auth": "^4.24.7", "@next-auth/prisma-adapter": "^1.0.7", "prisma": "^5.6.0" }
内容的提问来源于stack exchange,提问作者RJZ
相关产品推荐
相关产品推荐

