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

166
Visualizações
how to get a response from a post request

I have a post request. It is made and completed successfully, but I would like to know how I could use console.log(res.valor) in it, follows the data

consultarCorreio(){
  this.cartS.cart.valorTotalItems
  this.cartS.getCorreiosPrazoPreco(this.cartS.cart.pesoTotal, 80220060, this.formGeral.value.entrega_cep).subscribe((res: any) => {
    if (res.error) {
      this.modalS.createModal(MdAlertComponent, { title: 'Erro ao obter dados do período', message: 'Recarrega a página e tente novamente ou espere alguns minutos. Se o erro persistir, entre em contato com a nossa equipe.' });
      return;
    }
    console.log(res.data.valor)
    this.freteCorreio = res.data.valor
    console.log(this.freteCorreio)
  });
}

Returns this json (res)

{
"status": true,
"data": {
"cServico": {
  "Codigo": "04510",
  "Valor": "65,00",
  "PrazoEntrega": "6",
  "ValorSemAdicionais": "65,00",
  "ValorMaoPropria": "0,00",
  "ValorAvisoRecebimento": "0,00",
  "ValorValorDeclarado": "0,00",
  "EntregaDomiciliar": "S",
  "EntregaSabado": "N",
    "obsFim": {},
  "Erro": "0",
    "MsgErro": {}
   }
 }
}

How can I use console.log(res.Valor) to get the value of "Valor" (65.00) I tried using console.log(res.value) but it returned undefined.

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

0

You're getting undefined for console.log(res.value) because as you can see, your response doesn't have a value field. To get the value of field Valor use console.log(res.data.cServico.Valor). (that's because you have an object cServico embedded in the data object from your response).

about 4 years ago · Juan Pablo Isaza Relatório

0

So, I noticed some of your code is typescript, but I'm going to answer your question about console.log and the res object in javascript.

The following code assumes that your JSON response is exactly as you posted in your question and as follows:

{
  "status": true,
  "data": {
    "cServico": {
      "Codigo": "04510",
      "Valor": "65,00",
      "PrazoEntrega": "6",
      "ValorSemAdicionais": "65,00",
      "ValorMaoPropria": "0,00",
      "ValorAvisoRecebimento": "0,00",
      "ValorValorDeclarado": "0,00",
      "EntregaDomiciliar": "S",
      "EntregaSabado": "N",
      "obsFim": {},
      "Erro": "0",
      "MsgErro": {},
    },
  },
}

In this case, getting the Valor property in this would require accessing res.data.cServico.Valor. If you're receiving undefined, then I have to assume that these properties do not exist in your test (which means I'm not able to reproduce the problem).

This example can be seen in the following snippet:

const res = {
  "status": true,
  "data": {
    "cServico": {
      "Codigo": "04510",
      "Valor": "65,00",
      "PrazoEntrega": "6",
      "ValorSemAdicionais": "65,00",
      "ValorMaoPropria": "0,00",
      "ValorAvisoRecebimento": "0,00",
      "ValorValorDeclarado": "0,00",
      "EntregaDomiciliar": "S",
      "EntregaSabado": "N",
      "obsFim": {},
      "Erro": "0",
      "MsgErro": {},
    },
  },
};
console.log(res.data.cServico.Valor);

If you receive undefined, I would suggest checking the parent parameters until you get something other than undefined to trace the source of your error.

console.log(res.data.cServico);
console.log(res.data);
console.log(res);
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