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

589
Views
Next.js Error Serializing `results` returned from `getServerSideProps`

I'm working with Next.js trying to getServerSIdeProps and and getting this error:

Error: Error serializing .results returned from getServerSideProps in "/". Reason: undefined cannot be serialized as JSON. Please use null or omit this value.

export async function getServerSideProps(context) {
  const genre = context.query.genre;

  const request = await fetch(
    `https://api.themoviedb.org/3${
      requests[genre]?.url || requests.fetchTrending.url
    }`
  ).then((res) =>  res.json());
     
   return {
    props: {
      results: request.results,
    },
  };
}

It was working yesterday but today I'm getting this error. Could any body please help me?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

while converting your data to JSON an error was thrown because the value undefined can not be serialized in other words you can't turn objects with undefined as the value of one of their properties to json.

You need to turn the undefined value to null value for javascript to be able to serialize it to JSON(convert it to JSON).

The easiest way to turn undefined values to null in javascript is using a library called Lodash. Install Lodash using the commands below:

$ npm install lodash
// or
$ yarn add lodash

After that create the functions below:

import _ from 'lodash';

function prepareForSerializatoin(obj) {
    return obj.mapValues(obj, value => typeof value === 'undefined' ? null : value);
}

Finally, use this function while returning your response:

export async function getServerSideProps(context) {
  const genre = context.query.genre;

  const request = await fetch(
    `https://api.themoviedb.org/3${
      requests[genre]?.url || requests.fetchTrending.url
    }`
  ).then((res) =>  res.json());
     
   return prepareForSerializatoin({
    props: {
      results: request.results,
    },
  });
}
about 4 years ago · Juan Pablo Isaza 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!