I'm using an Observable
to return an object from service, that service checks if its item exist in cache otherwise it calls an http request and return the http's Observable
, in case the item exist in cache i'm using rxjs from
.
foo.service.ts
:
private foos: FooModelDTO[] = [];
getFoo(id: string) : Observable<FooModelDTO> {
var foo = this.foos.find(x => x.id === id);
if (foo) {
return from(foo); --> error!
}
return this._foosAPIService.getFooById(id);
}
foo-api.service.ts
:
getFooById(id: string) : Observable<FooModelDTO> {
return this._httpClient.get<FooModelDTO>(
this._configService.resourceApiURI + 'foos/' + id
);
}
foo.model.ts
:
export class FooModelDTO {
public id: string;
constructor (
id: string
)
{
this.id = id;
// other properties...
}
}
error TS2322: Type 'Observable<{}>' is not assignable to type 'Observable'. Type '{}' is missing the following properties from type 'FooModelDTO': id, imagePath, updateDate, title, and 6 more.
How to overcome this error? and return the cache result immediately in case it exist.
of
operator instead of usingfrom
. – micronyksfoo
fromfoos
array based on the index, not on theid
property inside the 'FooModelDTO' ? – Durgesh Palfind
– Shahar Shokrani