skip to Main Content

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:[email protected]:27354
    const error2 = new PrismaClientValidationError(renderErrorStr(validationCallsite));
                   ^
PrismaClientValidationError: 
Invalid `prisma.user.findUnique()` invocation in
C:GithubBackEndProjetosrcControllerAuthController.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:[email protected]:27354:20)
    at serializationFn (C:[email protected]:29815:19)
    at runInChildSpan (C:[email protected]:23564:12)
    at PrismaClient._executeRequest (C:[email protected]:29822:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async PrismaClient._request (C:[email protected]:29753:16)
    at async authenticate (C:GithubBackEndProjetosrcControllerAuthController.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.

2

Answers


  1. 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]'
           }
       })
    
    Login or Signup to reply.
  2. 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'
             }
             ~~~~~~~~~~~~~~~~~~~~~~~~~~
           }
         })
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search