Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prisma: TypeError: Do not know how to serialize a BigInt

I'm trying to fetch data from database and this is my prisma model:

  model instant_reports {
  id         BigInt    @id @default(autoincrement()) @db.UnsignedBigInt
  created_at DateTime?
  updated_at DateTime?
  deleted_at DateTime?
  timestamp  BigInt?
  client_id  BigInt?
  uniq_users BigInt?
}

So when i fetch data like this

  prismaService.instant_reports.findMany({
      skip: 0,
      take: 30,
    });

It throws error

TypeError: Do not know how to serialize a BigInt at JSON.stringify(<anonymous>)

And i don't even know how to deal with it, is there way to change data handler in findMany method?

If there is no rows in instant_reports so it gives me empty array without error, so the problem is in data with BigInt type

like image 813
bluepuper Avatar asked Dec 05 '25 17:12

bluepuper


1 Answers

It worked for me. Add this code to the beginning of yours.

BigInt.prototype.toJSON = function () {
  const int = Number.parseInt(this.toString());
  return int ?? this.toString();
};
like image 103
Alirezakvr Avatar answered Dec 08 '25 08:12

Alirezakvr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!