TypeError:无法读取未定义的属性“地图”

时间:2019-08-19 10:44:55

标签: javascript angular ionic-framework fullcalendar

我正在使用角度fullCalendar,但出现错误。

service.ts

getEvents()
  {
    return this.http.get('http:///api/lessons') .map( response => {
      return response.json().data.map((data) => {
            return {...data,start: data.start_time};
          });
   }); 
  }

home.ts

 ngOnInit(){
    this.events = this.service.getEvents().subscribe(events => {this.events = events;});
}

我遇到了这个错误:

  

TypeError:无法读取未定义的属性“地图”

2 个答案:

答案 0 :(得分:2)

您应该使用RxJS的map运算符。请注意,它与JavaScript的Array.map()不同。

此外,对于RxJS 6+,您应该使用pipeable operators,因此要使用pipe()函数。

import { map } from 'rxjs/operators';

getEvents() {
  return this.http.get('http:///api/lessons') 
    .pipe(
      map(response => {
        return response.map((data) => {
          return {...data,start: data.start_time};
        });
     })
  ); 
}

此外,在component.ts上,console.log()语句应在subscribe()内处理,因为HTTP请求本质上是异步的。将其放置在外部将导致其显示为undefined

ngOnInit(){
  this.service.getEvents().subscribe(events => {
    this.events = events;
    console.log(this.events);
  });
}

答案 1 :(得分:1)

看起来您正在使用旧的http, 请更新到HttpClient,因此您不需要

`return response.json().data.map((data) => {
        return {...data,start: data.start_time};
      });`

return response(data => { return {...data,start: data.start_time}; });