我有一个小问题,现在不知道它来自哪里。
我使用API并使用打字稿从阵容中的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
我不知道为什么它给了我一个错误 有什么办法吗?谢谢
答案 0 :(得分:0)
您的代码中lined
的值是什么。
你为什么这样做:
let fix = this.lineup['0']['data'].latest['data'][i];
this.fix = this.lineup['0']['data'].latest['data'];
如果在行1中定义了fix
,则只需使用修订而不是this.fix
,lined
变量也是如此。如果该值来自API或对对象进行迭代的方式不正确,则可能使这些声明使您的代码混乱。尝试在不同位置记录输出,并检查是否存在值。如果可以的话,可以共享一个punker或stackblitz链接。