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

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

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 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