简体   繁体   English

映射可观察对象时出现 Angular/RxJS 类型错误

[英]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: Datetitle: 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.

相关问题 Angular RxJS可观察到的加载错误 - Angular RxJS Observable loading error 从角度 4 中的“rxjs/Observable”导入 Observable 时出错 - Error in importing Observable from 'rxjs/Observable' in angular 4 在可观察对象中映射子数组时的 RxJS/角度错误 - RxJS/Angular error when mapping sub-array in observables 角度6 - Rxjs 6 - 类型可观察 <Object> 不能分配给Observable类型 <Item> - Angular 6 - Rxjs 6 - Type Observable<Object> is not assignable to type Observable<Item> 输入“可观察的<subscription> ' 不可分配给 Observable 类型<mydata> rxjs angular</mydata></subscription> - Type 'Observable<Subscription>' is not assignable to type Observable<MyData> rxjs angular 将Angular和RxJS 5迁移到6之后的错误-类型&#39;Observable &lt;{}&gt;&#39;无法分配给类型&#39;Observable &lt;…&gt;&#39; - Errors after migrated Angular and RxJS 5 to 6 - Type 'Observable<{}>' is not assignable to type 'Observable<…>' 带有Angular 2的RxJS 5:重试失败,但可观察到,但随后出现转发错误 - RxJS 5 with Angular 2: Retry failed Observable but then forward error angular2项目rxjs可观察到的错误 - angular2 project rxjs observable error Angular 9:rxjs 6:如何返回手动 Observable of Error - Angular 9 : rxjs 6 : how to return a manual Observable of Error Angular http RXJS observable 不调用错误回调 - Angular http RXJS observable not invoking error callbacks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM