簡體   English   中英

prisma.FindUnique 提供 JSON,預期字符串

[英]prisma.FindUnique provided JSON , expected string

我的棱鏡有問題,目前我使用 findUnique() function 到 select 注冊用戶基於他的 Z0C83F57C786A0B4A39EFAB23731C7EB

該錯誤描述它需要一個字符串而不是 JSON,但我不知道具體該怎么做,因為我在互聯網上看到的內容與我正在使用的內容相似。

控制台錯誤:

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...

認證 function:

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,這導致后端錯誤地接收數據。

您的查詢應如下所示:

而且在您的架構文件中,email 字段應標記為@unique

const user = await prisma.user.findUnique({
       where: {
         email: 'fany@gmail.com'
       }
   })

它應該是:

 const user = await prisma.user.findUnique({ where: email, });

或者像這樣,但看起來你在email正文中發送 object 而不是字符串:

 const user = await prisma.user.findUnique({ where: { email: email.email }, });

因為您的email變量是 object,所以 prisma 已經告訴您是哪一行導致了此錯誤:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM