RxJs Array of Observable to Observable of Array

The flatMap operator allows to do that. I don’t fully understand what you try to do but I’ll try to provide an answer… If you want load all the getPostsPerUser() { return this.http.get(“https://stackoverflow.com/users”) .map(res => res.json()) .flatMap((result : Array<User>) => { return Observable.forkJoin( result.map((user : User) => user.getPosts()); }); } Observable.forkJoin allows you to wait … Read more

How to return value inside subscribe Angular 4

You can’t directly return totalQuestions like that, you need to use a subject to achieve that. getTotalQuestions(idForm:string): Observable<string> { let totalQuestions:number; var subject = new Subject<string>(); this.getFirebaseData(idForm+”/Metadatos”) .subscribe(items => { items.map(item => { totalQuestions=item.Total; console.log(totalQuestions); subject.next(totalQuestions); }); } ); return subject.asObservable(); } Usage: getTotalQuestion(idForm).subscribe((r)=>console.log(r))

How to return observable from subscribe

You can’t return an observable from subscribe but if you use map instead of subscribe then an Observable is returned. canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> { // get route to be activated this.routeToActivate = route.routeConfig.path; // get user access levels return this._firebase.isUserAdminObservable .map(user => { // do something here // user.access_level; return true; }) .first(); // … Read more

How to check the length of an Observable array

You can use the | async pipe: <div *ngIf=”(list$ | async)?.length==0″>No records found.</div> Update – 2021-2-17 <ul *ngIf=”(list$| async) as list; else loading”> <li *ngFor=”let listItem of list”> {{ listItem.text }} </li> </ul> <ng-template #loading> <p>Shows when no data, waiting for Api</p> <loading-component></loading-component> </ng-template> Update – Angular Version 6: If you are loading up a … Read more

How to create an Observable from static data similar to http one in Angular?

Perhaps you could try to use the of method of the Observable class: import { Observable } from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; public fetchModel(uuid: string = undefined): Observable<string> { if(!uuid) { return Observable.of(new TestModel()).map(o => JSON.stringify(o)); } else { return this.http.get(“http://localhost:8080/myapp/api/model/” + uuid) .map(res => res.text()); } }

Merge subarrays using Observables

Provided it is a typo and the second element has subItems as well (and not searchProfiles), you don’t need flatMap or any thing of the sort, you can do it in a plain map using js array operators: var transformed = [].concat(…result.map(item => item.subItems)); or in your case httpResult$.map(result => [].concat(…result.map(item => item.subItems)) if the … Read more

Subscribe to observable is returning undefined

Maybe some pictures help? The numbers here indicate the order of operations. Send the Http Request Component is initialized and calls the getMovies method of the movieService. The movieService getMovies method returns an Observable. NOT the data at this point. The component calls subscribe on the returned Observable. The get request is submitted to the … Read more

Merging http observables using forkjoin

Try to use combineLatest instead of forkJoin : With combineLatest : const combined = Observable.combineLatest( this.http.get(‘https://testdb1.firebaseio.com/.json’).map((res: Response) => res.json()), this.http.get(‘https://testdb2.firebaseio.com/.json’).map((res: Response) => res.json()) ) combined.subscribe(latestValues => { const [ data_changes , data_all ] = latestValues; console.log( “data_changes” , data_changes); console.log( “data_all” , data_all); }); You can also handle it by forkJoin , but forkJoin will … Read more