You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prisma findUnique查询用户报错:期望字符串却传入JSON

解决Prisma findUnique()触发PrismaClientValidationError:期望字符串却传入JSON

问题现象

调用Prisma的user.findUnique()根据邮箱查询用户时,控制台抛出PrismaClientValidationError,核心提示为:Argument email: Got invalid value { email: 'fany@gmail.com', password: '1' } on prisma.findUniqueUser. Provided Json, expected String.

完整错误日志

C:\Github\BackEndProjeto\node_modules\@prisma\client\runtime\index.js:27354
    const error2 = new PrismaClientValidationError(renderErrorStr(validationCallsite));
                   ^
PrismaClientValidationError: 
Invalid `prisma.user.findUnique()` invocation in
C:\Github\BackEndProjeto\src\Controller\AuthController.ts:12:36

   9     email,
  10     password
  11 } = req.body;
→ 12 const user = await prisma.user.findUnique({
       where: {
         email: {
           email: 'fany@gmail.com',
           password: '1'
         }
         ~~~~~~~~~~~~~~~~~~~~~~~~~~
       }
     })

Argument email: Got invalid value
{
  email: 'fany@gmail.com',
  password: '1'
}
on prisma.findUniqueUser. Provided Json, expected String.


    at Document.validate (C:\Github\BackEndProjeto\node_modules\@prisma\client\runtime\index.js:27354:20)
    at serializationFn (C:\Github\BackEndProjeto\node_modules\@prisma\client\runtime\index.js:29815:19)
    at runInChildSpan (C:\Github\BackEndProjeto\node_modules\@prisma\client\runtime\index.js:23564:12)
    at PrismaClient._executeRequest (C:\Github\BackEndProjeto\node_modules\@prisma\client\runtime\index.js:29822:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async PrismaClient._request (C:\Github\BackEndProjeto\node_modules\@prisma\client\runtime\index.js:29753:16)
    at async authenticate (C:\Github\BackEndProjeto\src\Controller\AuthController.ts:12:18) {
  clientVersion: '4.2.1'
}
[nodemon] app crashed - waiting for file changes before starting...

后端认证代码

export class AuthController {
  async authenticate(req: Request, res: Response) {
    const {
        email,
        password
    } = req.body;
    const user = await prisma.user.findUnique({
        where: {
            email
        },
    });

    if(!user){
        return res.json({error: "User not found"})
    }
const isValuePassword = await compare(password, user.password)
if(!isValuePassword){
    return res.json({error: "Invalid Password"})
}

const token = sign({id: user.id}, "secret", {expiresIn: "1d"});
const {id} = user;
return res.json({user: {id, email}, token})
  }
}

问题原因及解决方案

问题根源是前端请求的JSON格式错误:前端传入了嵌套结构的JSON,导致后端解构req.body时,email变量接收的不是预期的字符串,而是包含email和password字段的对象。

错误的请求体示例:

{
  "email": {
    "email": "fany@gmail.com",
    "password": "1"
  }
}

正确的请求体应为扁平结构:

{
  "email": "fany@gmail.com",
  "password": "1"
}

修正前端请求格式后,后端就能正确解构出字符串类型的email参数,findUnique()查询即可正常执行。

内容的提问来源于stack exchange,提问作者Thiago David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 03:48:35