[英]Angular/RxJS type error when mapping observable
I am getting the following error when trying to map a resposne from my api尝试从我的 api 回复 map 时出现以下错误
Argument of type 'OperatorFunction<{ results: CustomerShipment[]; },{ title: string; start: Date; }[]>'
is not assignable to parameter of type 'OperatorFunction<CustomerShipment[], {title:string; start: Date;}[]>'.
Type '{ results: CustomerShipment[]; }' is missing the following properties from type 'CustomerShipment[]':
length, pop, push, concat, and 26 more.
API which returns a JSON array of objects with the following schema… I use Angular-Calendar v0.28.28 API 返回一个 JSON 对象数组,具有以下模式……我使用 Angular-Calendar v0.28.28
{
"0": {
"customerId": 1234,
"customer": "test",
"items": [
id: 123,
tssNumber: "1234567"
containerNumber: "e.g.",
appointmentTime: "23/03/2022 12:00 p.m."
]
}
}
An angular calendar event has the following required properties: start: Date
, title: string
. angular 日历事件具有以下必需属性: start: Date
, title: string
。 I am trying to map the response from my API to the CalendarEvent
object:我正在尝试 map 从我的 API 到CalendarEvent
object 的响应:
events$: Observable<CalendarEvent<{ results: CustomerShipment }>[]>;
ngAfterViewInit(): void
{
this.fetchCustomers(formatDate(this.viewDate, 'yyyy-MM-dd', 'en'));
}
fetchData(day: string): void {
let params = new HttpParams();
params = params.append('userid', this.getUserId());
params = params.append('date', day);
this.events$ = this.calendarService.getDailyCalendarShipments(params)
.pipe(
map(({ results }: { results: CustomerShipment[] }) => {
return results.map((result: CustomerShipment) => {
return {
title: result.customer,
start: new Date()
};
});
}));
}
then in the HTML component然后在 HTML 组件中
<div class="container-fluid">
<div #scrollContainer class="scroll-container" *ngIf="events$ | async; else loader; let events">
<app-day-view-scheduler (eventTimesChanged)="eventTimesChanged($event)"
(userChanged)="userChanged($event)"
[events]="events"
[headers]="headers"
[viewDate]="viewDate">
</app-day-view-scheduler>
</div>
</div>
I can do this without using the Observable, but I want to use the angular async
pipe.我可以在不使用 Observable 的情况下执行此操作,但我想使用 angular async
pipe。
I'm using the following example: https://mattlewis92.github.io/angular-calendar/#/async-events我正在使用以下示例: https://mattlewis92.github.io/angular-calendar/#/async-events
getDailyCalendarShipments
returning array of type CustomerShipment getDailyCalendarShipments
返回 CustomerShipment 类型的数组
Used from
to emit data in sequence and mergeMap
to subscribe this使用from
按顺序发出数据,使用mergeMap
订阅此数据
inside map
creating your event of type { results: CustomerShipment }
在map
创建类型为{ results: CustomerShipment }
的事件
toArray
creating { results: CustomerShipment }[]
toArray
创建{ results: CustomerShipment }[]
this.events$ = this.calendarService.getDailyCalendarShipments(params).pipe(
mergeMap(({ results }: { results: CustomerShipment[] }) => {
return from(results);
}),
map((result: CustomerShipment) => {
return {
title: result.customer,
start: new Date(),
};
}),
toArray()
);
You need either to change the type of the events$
Observable to be Observable<CalendarEvent<CustomerShipment>[]>
, or change the map
operator output to be CalendarEvent<{ results: CustomerShipment }>[]
like the following:您需要将events$
Observable 的类型更改为Observable<CalendarEvent<CustomerShipment>[]>
,或者将map
运算符 output 更改为CalendarEvent<{ results: CustomerShipment }>[]
,如下所示:
this.events$ = this.calendarService.getDailyCalendarShipments(params).pipe(
map(({ results }: { results: CustomerShipment[] }) => ({
results: results.map((result: CustomerShipment) => ({
title: result.customer,
start: new Date(),
})),
}))
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.