【问题标题】:How to show same data as my consolelog, when console log give good result?当控制台日志给出好的结果时,如何显示与我的控制台日志相同的数据?
【发布时间】:2019-04-16 13:27:28
【问题描述】:

我有一个小问题,我现在不知道它是从哪里来的。

我使用 API 并使用 typescript 从阵容中的 1 名玩家那里获取数据

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);


   }

但是问题,控制台日志给我:

  {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

我不知道为什么它给了我一个错误 有什么解决办法吗?谢谢

【问题讨论】:

    标签: angularjs typescript ionic3


    【解决方案1】:

    您的代码中lined 的值是多少。 你为什么这样做:

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

    如果fix在第1行定义,你只需要使用fix而不是this.fixlined变量也是如此。如果值来自 API 或者您迭代对象的方式不正确,您可能会让这些声明弄乱您的代码。尝试在不同的地方记录输出并检查值是否存在。如果可以的话,一个 plunker 或 stackblitz 链接将非常适合分享。

    【讨论】:

      猜你喜欢
      • 2018-03-27
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多