#Prisma Error with findUnique

1 messages · Page 1 of 1 (latest)

violet tusk
#

I am trying to look up a user by a unique field on the schema: "handle" and am getting this error shown in pic 1, and I have ensured that I have the @unique tag in the schema for that column, I was able to query on the displayName before just fine, and have even run prisma generate to ensure that my client is up to date.

violet tusk
#

i figured it out, the one thing I didn't account for when changing my schema is that my dynamic page route name matched that field and I had to change it to "handle"

subtle storm
#

Also just a little bonus tip, if User["handle"] is maybe a string, instead of typecasting it you might want to choose a strategy of dealing with that.

In this particular case instead of typing it as User["handle"] I might type it as string because semantically even though the User class maybe has a handle, you can't look for a user with a handle if no string is provided to this function.

#

Then you don't need an as string in there, and typescript will help you outside of this function if you are passing in something bad

#

for example if you have something like this in a loader

const formData = await request.formData()
const handle = formData.get("handle")

// typescript will scream at you because handle could potentially be undefined
const user = await getUserByHandleWithProfile(handle) 
#

then you could do your validations here to make sure that the data type is correct

const formData = await request.formData()
const handle = formData.get("handle")
if(typeof handle !=== "string" || handle.length < 1){
    throw new Error("Please provide a string.")
}

// Now typescript is happy and you've accidentally written better code
// as a byproduct  😀
const user = await getUserByHandleWithProfile(handle)