Home > Software engineering >  typescript isn't accepting that a number value receives a int value on prisma select?
typescript isn't accepting that a number value receives a int value on prisma select?

Time:09-20

i have a problem on a query in a prisma select

I have a function that receives the request and the response as parameters, and I am trying to insert the id passed as a parameter in the prism query, so that it returns the information of the informed user.

but when I use the id passed as a parameter, the typescript shows the following error:

(property) id?: number | undefined Type 'string' is not assignable to type 'number'.ts(2322) index.d.ts(1881, 5): The expected type comes from property 'id' which is declared here on type 'UserWhereUniqueInput'

i don't understand, because ID is set as a Int in the prisma schema , and the req.params.id is returning a number.

The function:

async showInfo(req: Request, res: Response){
const idPerson = req.params.id
console.log(idPerson)

const getUser = await prisma.user.findUnique({
  where: {
    id: idPerson,
  },
})

return res.json({getUser})


 }

CodePudding user response:

As the error says you're passing String where Number is expected.

async showInfo(req: Request, res: Response){
// typecast/convert string to Number
// make sure to add NaN check as Number("avc123") returns NaN.
const idPerson = Number(req.params.id)
console.log(idPerson)

const getUser = await prisma.user.findUnique({
  where: {
    id: idPerson,
  },
})

return res.json({getUser})


 }

P.S: I haven't ran this code but hopefully this typecasting should fix it.

  • Related