[ERROR] gặp lỗi TypeError: Do not know how to serialize a BigInt (ok)

Nếu dùng cách này nó báo lỗi vì nó sử dụng trường BigInt

Cách này ok;

import express, { urlencoded, json } from "express";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
const port = process.env.PORT || 8000;
const app = express();
app.use(urlencoded({ extended: true }));
app.use(json());
app.get("/", async (req, res) => {
  res.status(200).json({ msg: "Server is up and running" });
});
// BigInt.prototype.toJSON = function() { 
//   return this.toString() 
// }
Object.defineProperty(BigInt.prototype, "toJSON", {
  get() {
    "use strict";
    return () => String(this);
  }
});
app.get('/posts', async (req, res) => {
  const posts = await prisma.wp_posts.findMany({
    where: { post_status: 'publish' },
    select: { ID: true, post_name: true, post_title: true, post_content: true },
    take: 5
  });
  res.json(posts);
});
app.listen(port, () => {
  console.log(`Server is listening at port ${port}`);
});

Last updated

Was this helpful?