I have an http.get call in my service member.service.ts and I want to wait until that call completes before proceeding in app.component, so I'm trying to wrap the http.get call into an Observable for processing in app.component.
[Note: previously, before I wrapped the service in an Observable, the service was working fine . . . the http.get call on its own works as expected and returns a JSON object.]
But when I wrapped the http.get call in an Observable, as below, the application hangs. I searched online and read about flatMap, forkjoin, etc., but I'd like to keep the current structure, where the http.get call is in the member service, and all the other code is in app.component.
Many thanks if any ideas why it's hanging and how to fix!
member.service.ts
import { Injectable } from '@angular/core'; import { Observable, from, of } from 'rxjs'; import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root' }) export class MemberService { url: string = "http://localhost:3000/sign-up"; data: string = ''; constructor(private http: HttpClient) { } getMemberForm(): Observable<any> { let library = this.http.get(this.url,{responseType: 'json'}); library.subscribe((data) => { this.data = JSON.stringify(data); }); return of(this.data); } } app.component.ts
import { Component, OnInit, Injectable } from '@angular/core'; import { MemberService } from './member.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) @Injectable() export class AppComponent implements OnInit { got_data: boolean = false; data: string; constructor(private member: MemberService) {} ngOnInit() { this.member.getMemberForm().subscribe((data)=>{ this.data = data; if (this.data.length > 0) this.got_data = true; }); } }