Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
Error serializing Next.js in getStaticProps function?

I'm using getStaticProps() function and I get this error for no reason:

Error: Error serializing .posts[0] returned from getStaticProps in "/". Reason: object ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types.

I'm also using a mongoDb database, the connectDb() function runs the mongoose.connect() function and connects to the database. also, the console.log()s return valid JSON format data, I don't know what is causing this issue, here is my code:

export const getStaticProps: GetStaticProps = async (
  context: GetStaticPropsContext
) => {
  await connectDb()
  const count = await PostModel.countDocuments()
  const posts = await PostModel.find()
  console.log(posts)
  console.log(count)
  return {
    props: { posts: posts, count: count },
    revalidate: 10,
  }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use lean it will convert in a plain JavaScript object.

const posts = await PostModel.find().lean();

or you can try serialization via .toJSON

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!