P
Prisma•9mo ago
Rarity

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
No description
17 Replies
RaphaelEtim
RaphaelEtim•9mo ago
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:
const decimalValue = prismaDecimalObject.toString();
const numberValue = Number(decimalValue);
const decimalValue = prismaDecimalObject.toString();
const numberValue = Number(decimalValue);
To print the value of a Prisma.Decimal object, you should use the toString() method:
console.log(prismaDecimalObject.toString());
console.log(prismaDecimalObject.toString());
RaphaelEtim
RaphaelEtim•9mo ago
Prisma
Fields & types
Learn how to use about special fields and types with Prisma Client.
Rarity
RarityOP•9mo ago
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
RaphaelEtim
RaphaelEtim•9mo ago
Can you check the type of the object using the instanceof operator?
if (prismaDecimalObject instanceof Prisma.Decimal) {
console.log(prismaDecimalObject.toString());
} else {
console.log('Not a Prisma.Decimal object');
}
if (prismaDecimalObject instanceof Prisma.Decimal) {
console.log(prismaDecimalObject.toString());
} else {
console.log('Not a Prisma.Decimal object');
}
Rarity
RarityOP•9mo ago
it is false on my side 😄 so my obect is not a Prisma.Decimal
RaphaelEtim
RaphaelEtim•9mo ago
Yes it's not
Rarity
RarityOP•9mo ago
yeah but why lol i just store it with prisma into a postgres db 😦
RaphaelEtim
RaphaelEtim•9mo ago
Can you share the relevant part of your schema and the querying you are using to store and retrieve the decimal value?
Rarity
RarityOP•9mo ago
sure give me a second
Rarity
RarityOP•9mo ago
this is my schema
No description
Rarity
RarityOP•9mo ago
this is my method where i create the entry in the db
No description
Rarity
RarityOP•9mo ago
and this is the function call
No description
Rarity
RarityOP•9mo ago
while data.price is of type number
RaphaelEtim
RaphaelEtim•9mo ago
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 back
Rarity
RarityOP•9mo ago
yeah 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)) ?
RaphaelEtim
RaphaelEtim•9mo ago
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?
Rarity
RarityOP•9mo ago
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
Want results from more Discord servers?
Add your server