Sumchans
Sumchans

Reputation: 3774

Angular RxJS Observable loading error

I have declared an observable variable

workOrders: Observable<IWorkOrders[]>;

And then i load that observable this way.

  this.bs.getWorkOrders()
  .pipe(map(data:IWorkOrders[]) => this.workOrders = data)),
    .subscribe((data: IWorkOrders[]) =>
      // this.workOrders = data;
      this.woSubject.next(data),
  );

but when i do it i see a red squiggly under the this.workOrders = data line. And also when I hover it says [ts] Type 'IWorkOrders[]' is not assignable to type 'Observable' enter image description here

Upvotes: 1

Views: 298

Answers (2)

Antoniossss
Antoniossss

Reputation: 32517

workOrders=this.bs.getWorkOrders(); will work

workOrders is of type Observable. data is of type IWorkOrders[] Since those are 2 different types, thus error you are getting. You cannot assign IWorkOrders[] to Observable.

Upvotes: 1

Tsvetan Ganev
Tsvetan Ganev

Reputation: 8856

As the error message states - workOrders is of type Observable<IWorkOrders[]>, while the data variable is of type IWorkOrders[]. In your case you are trying to assign two incompatible types to one another. In the map function you are receiving the value of the Observable which is of type IWorkOrders[].

What you have to do, is to declare workOrders as an IWorkOrders[] instead of an Observable of that type:

workOrders: IWorkOrders[];

P.S. you have an extra trailing comma .pipe(map(data:IWorkOrders[]) => this.workOrders = data)), <--

Upvotes: 1

Related Questions