如何解析对象不能分配任何[]错误?

时间:2017-11-18 16:44:00

标签: angular typescript

在Ionic / Angular项目中,我如何解决

错误
  

'对象'不能分配给任何[]'

类型

到目前为止我的代码:

    export class HomePage {

  todayEvents: [];

  constructor( public dataService: DataServiceProvider) {
     this.getTodayEvents();     
  }

    getTodayEvents() {
    this.dataService.getTodayEvents().subscribe(events => {
      this.todayEvents = events;     
      console.log(events);
    });

  }


}

这就是在我的api调用中记录到控制台的内容:它看起来像一个数组,但我错过了一些东西,因为打字稿不喜欢它......

{data: Array(2)}
data
:
Array(2)
0
:
{id: 1, client_id: 1, starts_at: "2017-11-18 12:30:00", ends_at: "2017-11-18 14:30:00", completed: "3", …}
1
:
{id: 1, client_id: 1, starts_at: "2017-11-19 12:30:00", ends_at: "2017-06-27 14:30:00", completed: "3", …}

的DataService //我删除了.map,因为在新的common / Http中不再需要了。

getTodayAppointments() {
  return this.http.get(this.apiUrl);
    // No longer necessary in http/common ....map((res: Response) => res.json());  
}

1 个答案:

答案 0 :(得分:0)

尝试这样:

export class HomePage {

    todayEvents: Array<any> = [];

    constructor(public dataService: DataServiceProvider) {
        this.getTodayEvents();
    }

    getTodayEvents() {
        this.dataService.getTodayEvents().subscribe(events => {
            this.todayEvents = events.data;
            console.log(events);
        });

    }
}