2
My application is working perfectly, the only thing wrong is taking this error message;
MidiaExibirComponent.html:13 ERROR TypeError: Cannot read property 'nome' of undefined
at Object.eval [as updateRenderer] (MidiaExibirComponent.html:13)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14693)
at checkAndUpdateView (core.js:13807)
at callViewAction (core.js:14153)
at execComponentViewsAction (core.js:14085)
at checkAndUpdateView (core.js:13808)
at callViewAction (core.js:14153)
at execComponentViewsAction (core.js:14085)
at checkAndUpdateView (core.js:13808)
at callWithDebugContext (core.js:15056)
Line 13 is this line of code;
<td>{{ cotas.USD.nome }}</td>
This is my full page;
<table class="table">
<thead>
<tr>
<th scope="col">Moedas</th>
<th scope="col">Valor</th>
<th scope="col">Visualização</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ cotas.USD.nome }}</td>
<td>{{ cotas.USD.valor }}</td>
<td>{{ cotas.USD.ultima_consulta }}</td>
</tr>
<tr>
<td>{{ cotas.EUR.nome }}</td>
<td>{{ cotas.EUR.valor }}</td>
<td>{{ cotas.EUR.ultima_consulta }}</td>
</tr>
<tr>
<td>{{ cotas.ARS.nome }}</td>
<td>{{ cotas.ARS.valor }}</td>
<td>{{ cotas.ARS.ultima_consulta }}</td>
</tr>
<tr>
<td>{{ cotas.GBP.nome }}</td>
<td>{{ cotas.GBP.valor }}</td>
<td>{{ cotas.GBP.ultima_consulta }}</td>
</tr>
<tr>
<td>{{ cotas.BTC.nome }}</td>
<td>{{ cotas.BTC.valor }}</td>
<td>{{ cotas.BTC.ultima_consulta }}</td>
</tr>
</tbody>
</table>
That is the service;
import { Midia, Moedas } from './../core/model';
import { Http, Headers, URLSearchParams } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class MidiaService {
midiasUrl = 'http://localhost:8080/midia/midias';
quotationURL = 'http://api.promasters.net.br/cotacao/v1/valores?moedas=USD,BTC,EUR,ARS,GBP&alt=json';
constructor(private http: Http) { }
pesquisarUSD(): Promise<any> {
return this.http.get(`${this.quotationURL}`)
.toPromise()
.then(response => response.json().valores);
}
}
And that’s the component;
import { MidiaService } from './../midia.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-midia-exibir',
templateUrl: './midia-exibir.component.html',
styleUrls: ['./midia-exibir.component.css']
})
export class MidiaExibirComponent implements OnInit {
cotas = [];
constructor(private midiaService: MidiaService) { }
ngOnInit() {
this.pesquisar();
}
pesquisar() {
this.midiaService.pesquisarUSD()
.then(cotas => this.cotas = cotas);
}
}
==============================================================
json file
{
"status": true,
"valores": {
"USD": {
"nome": "Dólar",
"valor": 3.408,
"ultima_consulta": 1523995502,
"fonte": "UOL Economia - http://economia.uol.com.br/"
},
"EUR": {
"nome": "Euro",
"valor": 4.2171,
"ultima_consulta": 1523995502,
"fonte": "UOL Economia - http://economia.uol.com.br/"
},
"ARS": {
"nome": "Peso Argentino",
"valor": 0.1691,
"ultima_consulta": 1523995502,
"fonte": "UOL Economia - http://economia.uol.com.br/"
},
"GBP": {
"nome": "Libra Esterlina",
"valor": 4.8704,
"ultima_consulta": 1523995502,
"fonte": "UOL Economia - http://economia.uol.com.br/"
},
"BTC": {
"nome": "Bitcoin",
"valor": 27516,
"ultima_consulta": 1524047408,
"fonte": "Mercado Bitcoin - http://www.mercadobitcoin.com.br/"
}
}
}
===================================================== See verification code
.then(response => {
console.log(response.json().valores);
});
}
console score;
=====================================================================
.then(cotas => {
console.log(this.cotas = cotas);
});
How do I get this error message out?
The problem starts to show quota names that you don’t have, in the example of the question the quota
USD
. You have to try to see what you want to do in this case.– Isac
@wladyband Which of the objects is as Undefined?
cotas
orUSD
?– Jéf Bueno
@LINQ if you observe my code quotas is a variable created in the component class of angula and USD is part of the attributes coming from json URL, I updated my post take a look there.
– wladyband
@wladyband It would not be:
<td>{{ cotas.valores.USD.nome }}</td>
– Marcus Vinicius
@Marcusvinicius values is already being declared in the service class as shown in the post.
– wladyband
@wladyband Make a console.log in quotas after receiving the value and show the result then
– Jéf Bueno
@LINQ I guess I didn’t need it because I had already released the result of Json, but even so I put it as requested, please check the update of my post.
– wladyband
@wladyband It was supposed to be console.log in quotas
– Jéf Bueno
@LINQ ready check again please I just updated
– wladyband
You are sure that the variables are visible in the view scope?
– Jéf Bueno
@wladyband edited the answer: https://answall.com/a/292300/3635
– Guilherme Nascimento
Good morning friend, in this excerpt, how is your variable ? search() { this.midiaService.searchUSD() . then(quotas => this.quotas = quotas); } "quotas" is declared where and how?
– Herbert Junior