zero
zero

Reputation: 1725

Invalid prisma.user.findUnique() invocation

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
    }

Upvotes: 5

Views: 38085

Answers (6)

Kolade Chris
Kolade Chris

Reputation: 195

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!

Upvotes: 0

PLBx WooFzoO
PLBx WooFzoO

Reputation: 1

Correcting the Database URL worked for me.

Upvotes: 0

Ilan Zool
Ilan Zool

Reputation: 15

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

Upvotes: -1

Dipen Chand
Dipen Chand

Reputation: 145

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)

Upvotes: 6

Farrukh Faizy
Farrukh Faizy

Reputation: 1235

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()});

Upvotes: 0

Konrad
Konrad

Reputation: 287

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')

Upvotes: 5

Related Questions