Czołem!
Mam problem z odczytaniem elementów tablicy, wraz z obiektem, którą dostaję z zapytania.
O to jak wygląda tablica:
http://prntscr.com/ierx1f
Generalnie to co widać na screenie (czyli dane w konsoli) są wywoływane przez zapytanie :
console.log(this.myDataFromDb);
Gdy jednak będę próbować wejść "niżej" czyli :
console.log(this.myDataFromDb[0]);
dostaje w konsoli "undefined".
Próbowałem różnych wariacji jeżeli chodzi o wywołanie głębszych danych ale niestety bezskutecznie.
Tutaj zapodaje kolejno mój komponent wraz z serwisem:
@Component({
selector: 'app-frienddetails',
templateUrl: './frienddetails.component.html',
styleUrls: ['./frienddetails.component.css']
})
export class FrienddetailsComponent implements OnInit {
name:string;
id:number;
myDataFromDb: any[] = [];
constructor(public friendService: FriendService, private route: ActivatedRoute) {
this.name = this.route.snapshot.params['name'];
}
fetchListOfMatch(query){
this.friendService.getListOfMatch(query).subscribe((data) =>
this.myDataFromDb.push(data)
);
}
ngOnInit() {
this.id = this.friendService.getIdByName(this.name);
this.friendService.id = this.id;
this.friendService.query = `${this.friendService.apiUrl}${this.friendService.id}?season=11&${this.friendService.apikey}`;
console.log(this.myDataFromDb);
this.fetchListOfMatch(this.friendService.query);
}
}
@Injectable()
export class FriendService{
friendArray: any;
apikey = APIKEY;
id:number;
dataFromDb:any=[];
apiUrl = "https://eun1.api.riotgames.com/lol/match/v3/matchlists/by-account/";
query : string;
constructor(private http: Http){
this.friendArray = FRIEND;
}
getIdByName(name:string){
for(let i of this.friendArray)
{
if(i.nick == name)
{
return i.account_id;
}
}
}
getListOfMatch(query): Observable<any>{
return this.http.get(query)
.map(res => res.json())
.do(res => this.dataFromDb[query] = res)
}
}