Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

592
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda