Invalid prisma.user.findUnique() invocation
Asked Answered
U

6

5

I can't seem to find what the error in the invocation is

function findUser(req, res) {
    const username = req.body.username;
    
    prisma.user.findUnique({
        where: { username: username },
        select: { username: true }
    })
    .then(data => {
        res.send({
            'userExists': data ? true : false
        })
    })
    .catch(err => {
        res.status(500).send({
           message: err.message || "Some error occurred while retrieving user."
        })
    })
    .finally(async () => { await prisma.$disconnect()});

// schema.prisma

    datasource db {
      provider = "postgresql"
      url      = env("DATABASE_URL")
    }

    generator client {
      provider = "prisma-client-js"
    }

    model User {
      id Int @default(autoincrement()) @id
      username String @unique
      password String
      salt String
      createdAt DateTime @default(now())
      updatedAt DateTime @updatedAt
    }
Unilingual answered 6/5, 2021 at 1:37 Comment(0)
E
6

It might be the late response. I was also running into the same problem. But documentation saved my day. https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findunique

You just have to add @unique attribute to your username field in schema.prisma, like below —

username String @unique @db.VarChar(255)

Engraving answered 11/11, 2022 at 8:50 Comment(1)
This seems to be the right answer. Add @unique and also re-run npx prisma generateNodababus
O
5

From Prisma side everything is OK. The problem is probably req.body.username, if it's undefined you receive Invalid 'prisma.user.findUnique()' invocation.
You have to add validation for username, i.e.

if {typeof username !== string} return res.status(404).send('invalid username')
Ostia answered 8/5, 2021 at 19:17 Comment(0)
C
0

After looking at your code it seems that the username is missing from your req.body. I would suggest always verify the params you want to extract from req.body. I refactor your code to es6.

Here is the updated code you can try,

function findUser(req, res) {
    // Destructure username from req.body
    const { username } = req.body;

    if(username == null) throw new Error('Username undefined');
    
    // when property and value is same, you can write 'username' like below
    prisma.user.findUnique({
        where: { username },
        select: { username: true }
    })
    .then(data => {
        res.send({
            'userExists': data ? true : false
        })
    })
    .catch(err => {
        res.status(500).send({
           message: err.message || "Some error occurred while retrieving user."
        })
    })
    .finally(async () => { await prisma.$disconnect()});
Chef answered 9/5, 2021 at 0:8 Comment(0)
W
0

Correcting the Database URL worked for me.

Weakfish answered 31/3 at 14:22 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Physiotherapy
F
0

I got the same error with findMany while using Prisma with MongoDB.

After spending several hours debugging, it happened it was an IP address issue. So I went to the Network access tab on my MongoDB database and allowed access everywhere and boom, error resolved!

Faison answered 15/6 at 7:21 Comment(0)
I
-1

I had the same error and this solved it in my case

not working:

        const projects = await prisma.project.findMany();
        return projects;

working:

        const projects = await prisma.project.findMany();
        if (projects) {
            return projects;
        }

Before the user creates projects, 'projects' will always be empty so returning it causes an error

Institutor answered 19/10, 2023 at 8:30 Comment(3)
What does "empty" mean exactly?Sixpence
By empty I meant that its table(in my case 'project') doesn't contain any rows of data.Institutor
Then this if statement will achieve nothingSixpence

© 2022 - 2024 — McMap. All rights reserved.