3

I have a method:

getFilters(): Observable<Filters[]> {
    let filters: Observable<Filters[]> = [
      {
        property: "Value",
        property2: "Value2"
     },
     {
       property: "Value3",
       property2: "Value4"
     }
  return filters;
}

I get an error:

Type '{Property: string, Property2: string} is not assignable to type Observable.

Fateme Fazli
  • 11,582
  • 2
  • 31
  • 48
sander
  • 1,426
  • 4
  • 19
  • 46

3 Answers3

5

You need to create an Observable:

import { Observable, of } from "rxjs";

getFilters(): Observable<Filters[]> {
    let filters: Filters[] = [
      {
        property: "Value",
        property2: "Value2"
      },
      {
        property: "Value3",
        property2: "Value4"
      }
     ];
    return of(filters);
    }
Prachi
  • 3,478
  • 17
  • 34
mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52
0

This is how observable works:

getFilters(): Observable<Filters> {
    let filters = [
      {
        property: "Value",
        property2: "Value2"
     },
     {
       property: "Value3",
       property2: "Value4"
     }
    ];
  return Observable.from(filters);
}

You can use it like:

// Prints out each item
let subscription = this.getFilters().subscribe(
x => console.log('onNext: %s', x),
e => console.log('onError: %s', e),
() => console.log('onCompleted'));

// => onNext: {property: "Value",property2: "Value2"}
// => onNext: {property: "Value3",property2: "Value4"}

Refer to this for more details.

Vikas
  • 11,859
  • 7
  • 45
  • 69
Prachi
  • 3,478
  • 17
  • 34
0

I got it working by adding to the imports:

import { Observable, of } from "rxjs";

Then doing

return of(filters);
sander
  • 1,426
  • 4
  • 19
  • 46