Pierre Torres
Pierre Torres

Reputation: 287

How to show same data as my consolelog, when console log give good result?

I have a small problem and I don't now where it comes from.

I use an API and with typescript i get data from 1 player in lineup

this.http.get("https://soccer.sportmonks.com/api/v2.0/teams/"+ this.teamid +"?api_token=9f2m43uMDPMzrQGeE8Kv9PmHQLRdu60VrTK6oWXllRvJoVIkr35s&include=latest:limit(10|1).lineup,latest.lineup,latest.localTeam,latest.visitorTeam ")


.subscribe(result => {

    this.lineup = result.json();
    this.lineup = Array.of(this.lineup);

    console.log(this.lineup);


     for(let i = 0; i < this.lineup['0']['data'].latest['data'].length; i++){
     let fix = this.lineup['0']['data'].latest['data'][i];
     this.fix = this.lineup['0']['data'].latest['data'];
    console.log("fix",this.fix);

    let lined = this.fix[i].lineup['data'][i];
    this.lined = this.fix[i].lineup['data'];
    this.lined = this.lined.filter(lined => lined.player_id === 580 && lined.stats.length != 0);
    this.cards = this.lined[0].stats;



    console.log("lined",this.cards);


   }

But the problem, console log give me :

  {shots: {…}, goals: {…}, fouls: {…}, cards: {…}, passing: {…}, …}
cards: {yellowcards: 0, redcards: 0}
fouls: {drawn: 1, committed: 1}
goals: {scored: 1, conceded: 0}
other: {assists: 0, offsides: 0, saves: 0, pen_scored: 0, pen_missed: 0, …}
passing: {total_crosses: 0, crosses_accuracy: 0, passes: 34, passes_accuracy: 87}
shots: {shots_total: 11, shots_on_goal: 3}
__proto__: Object

There is all Information what I need but I have this error : 

Cannot read property 'stats' of undefined


This is api result, if i delete .stats at this.cards = this.lined[0].stats;

additional_position: null
fixture_id: 10420139
formation_position: 11
number: 7
player_id: 580
player_name: "Cristiano Ronaldo"
position: "F"
posx: null
posy: null
stats: {shots: {…}, goals: {…}, fouls: {…}, cards: {…}, passing: {…}, …}
team_id: 625
__proto__: Object

I don't know why it gave my an error Any solution? Thanks

Upvotes: 2

Views: 40

Answers (1)

nircraft
nircraft

Reputation: 8478

what is the value for lined in your code. Why are you doing:

let fix = this.lineup['0']['data'].latest['data'][i];
 this.fix = this.lineup['0']['data'].latest['data'];

if fix is defined in line 1, you just need to use fix not this.fix, same goes with lined variable. You may have these declarations messing up your code if the value is coming from API or the way you are iterating over the object is not right. Try logging the output at different places and check if value is present. A plunker or stackblitz link would be great to share if you can.

Upvotes: 1

Related Questions