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

173
Visualizações
JSON string won't print to console

I have this service:

getCert(p: string): Promise<string> {
  return ApiService.getData(this.apiUrl + p + this.certEndpoint).then(
    (response) => response.data
  );
}

This is an example of data:

{
    "state": "success",
    "message": "Message received."
}

If I return response.data.state or response.data.message I get the relevant value.

If I call it with this:

async componentDidMount() {
    const queryString = require('query-string');
    const parsed = queryString.parse(location.search);
    return this.docListingService.getPks(parsed.policy).then(async pks => {
        this.setState({ isLoading: false, packs });
        this.certService.getCert(parsed.policy).then( response => {
            console.log('RESPONSE: ' + response);
        });
        return pks;
    }).catch((error) => {
        this.loggingService.logError('Error returning Docs ' + error);
        this.setState({ errorOccured: true});
    });
}

response is a string but my consle.log prints RESPONSE [object Object]. I tried console.log('RESPONSE: ' + response.state); and console.log('RESPONSE: ' + response.message); but they error. As does:

console.log('RESPONSE: ' + JSON.parse(response));

How do I resolve this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

console.log() accepts as many arguments as you like. So you can just use it like this:

console.log('response ', response)

If you want to log multiple variables, you can do it like this:

console.log(variable1, variable2, variable3, 'some string', variable4);

about 4 years ago · Juan Pablo Isaza Relatório

0

Just use:

JSON.stringify(response)

This will show the Object structure as string value.

about 4 years ago · Juan Pablo Isaza Relatório

0

you have a bug in console.log of your code, you are trying to concat a string and json object. you can try this code

console.log('response ', JSON.parse(response));

//or

console.log('response ', JSON.parse(response).data.message);

//or

console.log('response ' + JSON.parse(response).data.message);
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