1

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@Sumchans 2 different types - thus error - iv edited answer.
so you saying if i declare this way workOrders:IWorkOrders, i can call it like this from my component this.bs.getWorkOrders().subscribe.
Yep you can if you rly need Obeservable in your component insteed of emited value.
Okay thanks, still new to angular & observables, totally confusing concepts.
Observable is littlebit like plain old listener pattern + streams
1

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)), <--

4 Comments

I tried that it worked that way, then how do i subscribe to the workOrders variable from another component if it is not an observable.
There are many different ways to pass data to other components, depending on what exactly you want to achieve.
Doesn't it have to be an observable for me to subscribe from other components
Please explain what you want to do because it's not clear by looking at just 3 lines of code with no context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.