Home > Software design >  prisma.FindUnique provided JSON , expected string
prisma.FindUnique provided JSON , expected string

Time:08-27

I'm having a problem with the prisma, at the moment where I use the findUnique() function to select the registered user based on his email.

The error describes that it expects a string and not a JSON , but I don't know exactly how to do that, because the content I saw on the internet is similar to what I'm using.

Console error:

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: '[email protected]',
           password: '1'
         }
         ~~~~~~~~~~~~~~~~~~~~~~~~~~
       }
     })

Argument email: Got invalid value
{
  email: '[email protected]',
  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...

Authentication 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})
  }
}

EDIT

I resolve it, in the frontend request, i was sending a JSON inside of another JSON , this caused the backend to receive the data wrongly.

CodePudding user response:

Your query should look something like this:

And also in your schema file the email field should be marked as @unique

const user = await prisma.user.findUnique({
       where: {
         email: '[email protected]'
       }
   })

CodePudding user response:

It should be:

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

Or like this, but look like you send an object in email body instead of string:

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

Because your email variable is an object, prisma already told you about which line causing this error:

→ 12 const user = await prisma.user.findUnique({
       where: {
         email: {
           email: '[email protected]',
           password: '1'
         }
         ~~~~~~~~~~~~~~~~~~~~~~~~~~
       }
     })

  • Related