Prisma.Decimal is not a Decimal
i am using prisma with postgresql and i got a lot of problems with prisma.decimal.
i tried to store it as new Prisma.Decimal(number Input) and when i get it back from Prisma, i get an [object Object] there is no way i can convert this into a Decimal. Every Method -> toNumber() etc. results in a error, that this is not a function.
So i tried to do the following Prisma.Decimal.IsDecimal(Object form Database), as u can see the result is false.
How can i convert this Prisma.Decimal from postgresql into a decimal which i can use in my code.
Please help!
Best Regards
Steven
17 Replies
Hi @Rarity 👋
Prisma.Decimal
is not a native JavaScript type and it doesn't support native JavaScript operations. When you retrieve a Decimal type from the database using Prisma, it returns a Prisma.Decimal
object. This object is not a native JavaScript number or string, and it doesn't support native JavaScript operations like addition or conversion to number directly. Instead, you need to use methods provided by the Decimal.js
library that Prisma uses under the hood.
Here's how you can convert a Prisma.Decimal object to a number:
To print the value of a Prisma.Decimal
object, you should use the toString()
method:
hey, if i do the prismaDecimalObject.toString() i get a return Value -> [object Object] and no the number thats why i am confused so much
and therefore i get a Number(prismaDecimalObject.ToString()) -> NaN
Can you check the type of the object using the
instanceof
operator?
it is false on my side 😄 so my obect is not a Prisma.Decimal
Yes it's not
yeah but why lol i just store it with prisma into a postgres db 😦
Can you share the relevant part of your schema and the querying you are using to store and retrieve the decimal value?
sure give me a second
this is my schema
this is my method where i create the entry in the db
and this is the function call
while data.price is of type number
You are returning
data.price
and not the value of price stored in priceHistoryEntry
variable which should hold the correct price you want to return backyeah but data.price is a number, data.price = 10 for example and i call new Prisma.Decimal(10), or do i need to call new Prisma.Decimal(Number(data.price)) ?
No you don't have to do that. The
data.price
you are returning is a number and does not need any casting. My question is why are you assinging data.price
to price
in the return block? Is the value of price not suppossed to come from the variable priceHistoryEntry
?na the data.price comes from a input box, so u enter the price there, and after that i create a new pricehistoryentry with the new prisma.decimal