Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
JSON.stringify() not working, but console.log() returns JSON

So I am making a request to an API endpoint. When I print JSONBody which is the variable data passed to POSTRequest(), I get the JSON I created printed to the log, however when I then try and JSON.Stringify() it, I get returned an empty object?

Was just wondering what I am doing wrong and how to fix it :)

getFileData()

const getFileData = () => {
    var data = {}

    // DO SOME STUFF HERE TO data

    POSTRequest(data);
}

POSTRequest()

const POSTRequest = async (JSONBody) => {
    console.log(JSONBody)
    console.log(JSON.stringify(JSONBody))
    try {
        const response = await fetch(API_ENDPOINT, {
            method: 'POST',
            body: JSON.stringify(JSONBody),
            headers: { 'Content-Type': 'application/json' }
        });
        response.json()
            .then(function (res) {
                Promise.resolve(res)
                    .then(function (finalData) {
                        console.log(finalData);
                        props.setData(finalData);
                    });
            });
    } catch (error) {
        console.log(error);
    }
} 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're not waiting for the response from the fetch call correctly, try this:

const POSTRequest = async (JSONBody) => {
    console.log(JSONBody)
    console.log(JSON.stringify(JSONBody))

        await fetch(API_ENDPOINT, {
            method: 'POST',
            body: JSON.stringify(JSONBody),
            headers: { 'Content-Type': 'application/json' }
        }).then((response) => {
           console.log(response);
           props.setData(response);
       }).catch((error) => {
          console.log('POSTRequest error: ' + error)
       })
   });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda