Issue
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
.
Solution
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);
Answered By - phentnil
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.