I think I am missing something here, since I find using Angular http.get with pipe(map) from RXJS quite counter intuitive:
The backend returns an array of simple objects (see Unifiedparams interface below). I want to extract an Observable<String[]> and pass it on to the app.component.html, where I use *ngFor="let param of params$ | async" to build a dropdown.
I can't seem to be able to access the name attribute without nested maps although the returned json is a single dimension array.
fetchParams(): Observable<String[]>{
const result = this.http.get<unifiedParam[]>('http://localhost:5000/getparams');
const params:Observable<String[]> = result.pipe(map(unifiedParams => res.map(unifiedParam=>unifiedParam.name)));
return params;
}
This seems to indicate that the Observable is an array itself but using this.http.get<unifiedParam> and rows:Observable<String> respectively results in rows being empty:
fetchParams(): Observable<String>{
const result = this.http.get<unifiedParam>('http://localhost:5000/getparams');
const params:Observable<String> = result.pipe(map(unifiedParam=> unifiedParam.name)));
params.pipe(tap(console.log)); //doesn't print anything
return params;
}
So my question here is if using Observable<UnifiedParam[]> and .pipe(map(res=>map(el=>el.name) is the way to go.
Thanks!
Update: Forgot the interface :)
interface unifiedParam { name: String, source: String };
Update 2: To clarify what puzzles me: Why do I need the outer map() function. Isn't map() defined for arrays?
params.pipe(tap(console.log));not working because you are not subscribing to it, you have to change it like:return params.pipe(tap(console.log))