typescript 错误类型错误:无法读取未定义的属性“地图”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46112408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERROR TypeError: Cannot read property 'map' of undefined
提问by mchev
got this error and really don't get why.. I'm trying to display events on angular-calendar : https://mattlewis92.github.io/angular-calendar/#/async-events
得到这个错误,真的不明白为什么..我正在尝试在 angular-calendar 上显示事件:https: //mattlewis92.github.io/angular-calendar/#/async-events
error_handler.ts:1 ERROR TypeError: Cannot read property 'map' of undefined at MapSubscriber.project (planning.component.ts:100) at MapSubscriber._next (map.ts:75) at MapSubscriber.Subscriber.next (Subscriber.ts:95) at MapSubscriber._next (map.ts:80) at MapSubscriber.Subscriber.next (Subscriber.ts:95) at XMLHttpRequest.onLoad (xhr_backend.ts:99) at ZoneDelegate.webpackJsonp.85.ZoneDelegate.invokeTask (zone-mix.js:424) at Object.onInvokeTask (ng_zone.ts:280) at ZoneDelegate.webpackJsonp.85.ZoneDelegate.invokeTask (zone-mix.js:423) at Zone.webpackJsonp.85.Zone.runTask (zone-mix.js:191)
error_handler.ts:1 错误类型错误:无法读取 MapSubscriber.project (planning.component.ts:100) at MapSubscriber._next (map.ts:75) at MapSubscriber.Subscriber.next (Subscriber.ts) 的未定义属性“地图” :95) 在 MapSubscriber._next (map.ts:80) 在 MapSubscriber.Subscriber.next (Subscriber.ts:95) 在 XMLHttpRequest.onLoad (xhr_backend.ts:99) 在 ZoneDelegate.webpackJsonp.85.ZoneDelegate.invokeTask (zone -mix.js:424) at Object.onInvokeTask (ng_zone.ts:280) at ZoneDelegate.webpackJsonp.85.ZoneDelegate.invokeTask (zone-mix.js:423) at Zone.webpackJsonp.85.Zone.runTask (zone- mix.js:191)
component.ts
组件.ts
import { Component, ChangeDetectionStrategy, OnInit } from '@angular/core';
import { Http, URLSearchParams } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import { CustomDateFormatter } from './custom-date-formatter.provider';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
interface Event {
id: number;
title: string;
start: string;
end: string;
}
events$: Observable<Array<CalendarEvent<{ event: Event }>>>;
constructor(private http: Http) { }
ngOnInit(): void {
this.fetchEvents();
}
fetchEvents(): void {
this.events$ = this.http
.get(this.apiUrl)
.map((response) => response.json())
.map(({ results }: { results: Event[] }) => {
return results.map((event: Event) => {
return {
title: event.title,
start: new Date(event.start),
end: new Date(event.end),
color: colors.yellow
};
});
});
}
json data from api
来自api的json数据
[
{
"id": 2,
"user_id": 1,
"planning_id": 1,
"start": "2017-09-03T22:00:00.000Z",
"end": "2017-09-06T12:33:46.271Z",
"title": "A 3 day event",
"created_at": "2017-09-05 16:39:47",
"updated_at": "2017-09-05 16:39:47"
},
{
"id": 3,
"user_id": 1,
"planning_id": 1,
"start": "2017-09-03T22:00:00.000Z",
"end": "2017-09-06T12:33:46.271Z",
"title": "A 3 day event",
"created_at": "2017-09-07 13:27:36",
"updated_at": "2017-09-07 13:27:36"
}
]
回答by Francesco Guidi
I know it's old but I found a way to make it work with Angular 5
我知道它很旧,但我找到了一种方法让它与 Angular 5 一起使用
Declare the events this way:
以这种方式声明事件:
asyncEvents$: Observable<CalendarEvent[]>;
Then load your data with HttpClient
然后使用 HttpClient 加载您的数据
Note that I have a DateEvent
returned by my API
请注意,DateEvent
我的 API 返回了一个
loadEvents() {
this.asyncEvents$ = this.http.get<DateEvent[]>(YOUR_URL)
.map(res => {
return res.map(event => {
return {
title: event.label,
start: new Date(event.startDate),
color: {primary: event.color, secondary: "#D1E8FF"},
meta: {
event
},
allDay: true
};
});
});
}
Then everything works as expected
然后一切都按预期进行
回答by Ganesh Singh
- ng update
- ng update @angular/cli
- ng update @angular/core
- 更新
- ng更新@angular/cli
- ng更新@angular/core
回答by SKL
Remove node_module
and run npm install
. Its solved my issue
删除node_module
并运行npm install
。它解决了我的问题
回答by SplitterAlex
You have to import the map operator:
您必须导入地图运算符:
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/map';