0
Thanks in advance for your attention.
I am having difficulty popular an object list returned from an API in Angular 7. My service class is returning the service data, but I can’t get popular in my object list (returned from service). Below are details of my implementation.
Json
{
"sucess":true,
"message":"Sucesso",
"data":[
{
"codigoPlanoContratacao":"DGC1",
"codigoCobertura":223,
"descricaoCobertura":"ASSISTENCIA DOMICILIAR II",
"codigoTipoRisco":0,
"valorImportanciaSegurada":0,
"valorPremioLiquido":24.0
},
{
"codigoPlanoContratacao":"DGC1",
"codigoCobertura":400,
"descricaoCobertura":"INC./RAIO/EXPL./QUEDA AERONAVE",
"codigoTipoRisco":0,
"valorImportanciaSegurada":50000,
"valorPremioLiquido":26.25
},
{
"codigoPlanoContratacao":"DGC1",
"codigoCobertura":404,
"descricaoCobertura":"VENDAVAL/IMPACTO VEICULO",
"codigoTipoRisco":0,
"valorImportanciaSegurada":10000,
"valorPremioLiquido":31.09
}]
}
My service.ts
TodasCoberturas() : Coberturas[] {
this.http.get<Coberturas[]>(this.UrlServiceV1 + "combinadoPlano?codigoTipoRisco=1")
.subscribe(
data => {
debugger;
this.coberturas = data;
});
return this.coberturas;
}
Class Coverage
export class Coberturas{
codigoPlanoContratacao: string
codigoCobertura: number
descricaoCobertura: string
codigoTipoRisco: number
valorImportanciaSegurada: number
valorPremioLiquido: number
}
componenet.ts
public coberturas: Coberturas[];
constructor(public coberturasServices: CoberturasServices) {
this.coberturas = this.coberturasServices.retornarListaDetalhe();
}
html
But in Debugger, I can see my object list, however I can’t get popular in my variable. I tried adding objects in the returned data and I did not succeed, as it informs that my variable is Undefined.
Thank you again.
this.covers = date.date;
– Marconi
@Marconi, data.data is not recognized. Below link of the image http://prntscr.com/lqnkrd
– Bruno Leite
Bruno,
console.log(data)
what appears?– Marconi
@Marconi, using the console.log as shown below, I have the service data in Browse Browser http://prntscr.com/lqov4m Code http://prntscr.com/lqovw5
– Bruno Leite
I have an example here at Github when I was studying angular, take a look at the link I left.
– Marconi
One of the examples, in service:
ListarPessoa(): Observable<Pessoa[]> {
 return this._http.get<Pessoa[]>(
${Api_pessoa}/Pessoa);
 }
. In the componentthis.servico.ListarPessoa()
 .subscribe(resposta => this.pessoa = resposta)
– Marconi
@Marconi will take a look at the code. However I have tried several examples on the net and can not popular the list of my service in my object Covers[].
– Bruno Leite
Bruno, if I’m wrong you have to use Observable, I just can’t remember why, but I know you have.
– Marconi
@Marconi the link that Voce passed, had already made this implementation before and the problem persisted. After the cursor goes through my object, it still remains undefined.
– Bruno Leite
Guys, I’ve tried every solution I’ve found on the net and I can’t fix it. Someone can help me with something. Thank you!
– Bruno Leite
Bruno will give you one last suggestion:
.subscribe((data) => { this.pessoa = data['data']});
I got it from the documentation of angular– Marconi
A question you have some error in the console? has used safe navigation operator(
?
)?– Marconi
I believe your request is fine, the problem is in its component
app-gridCobertudas
– Marconi
@Marconi, I have no mistake on the console. The operator (?), I am not using my object Covers, as I told you earlier is not being loaded.
– Bruno Leite
@Marconi, I will create a new project with just the default page and see if I can play the dice on the screen. I’ll let you know after the test. Thanks for your attention.
– Bruno Leite
Bruno, I remembered you had a project here at stackblitz, in the address zip code field I have a request for viaCep that can be seen on
app.service.endereco.ts
methodGetEndereco
. In theendereco.component.ts
I call the method, take a look maybe help you!– Marconi
@Marconi, thank you. I’ll analyze.
– Bruno Leite