打字稿:未捕获的类型错误this.reduce不是函数

时间:2019-05-21 12:30:41

标签: javascript angular typescript

我的代码以前工作正常,使用reduce函数后正确显示了API中的数据。

我从github上的新计算机上提取了文件,突然出现此错误。我们将尽一切努力找出我做错了什么,因此对您的任何帮助都将不胜感激。

"ERROR TypeError: tickets.reduce is not a function
at SectionDashboardComponent.push../src/app/Sections/section- 
dashboard/section- 
dashboard.component.ts.SectionDashboardComponent.getTicketData (section-dashboard.component.ts:30)"

在ts页面上似乎发生了此错误:

import { Component, OnInit } from '@angular/core';
import { freshServiceService } from 
'src/app/Services/freshservice.service';
import { Ticket } from 'src/app/Domain/Ticket';

 @Component({
 selector: 'app-section-dashboard',
 templateUrl: './section-dashboard.component.html',
 styleUrls: ['./section-dashboard.component.css']
})
 export class SectionDashboardComponent implements OnInit {

constructor(private _freshServiceService: freshServiceService) { }

private ticketCounts: number[];
private ticketResponders: string[];

ngOnInit() {
this._freshServiceService.fetchTickets().subscribe
(
  data =>
  {
    console.log(data);
    this.getTicketData(data);
  }
);
}

private getTicketData(tickets: Ticket[]): void {

const mappedTickets = tickets.reduce((x, y) => {
  {x[y.responder_name] = x[y.responder_name] + 1 || 1};
  return x;
}, []);

this.ticketResponders = Object.keys(mappedTickets);

this.ticketCounts = Object.values(mappedTickets);

console.log(this.ticketResponders);
console.log(this.ticketCounts);
}
}

0 个答案:

没有答案