0
votes

Following is my api call, I have interceptor in between, so I am getting this data from localstorage. The data is stringified and saved. When I return it I convert into observable by observable.of(cachedata).

  getApplications(userid: string): Observable<any> {
    let data =this.httpclient.get(this._getAPIUrl('user/' + userid + '/applications'))
      .map((res: Response) => res.json())
      return data;
  }

I called it from component

  ngOnInit() {
        const userid = this.platformService.currentUserId;
        console.log(this.platformService.getApplications(userid));
}

it print result

Observable {_isScalar: false, source: Observable, operator: MapOperator}

if I subscribe this call data didn't get available.

 this.platformService.getApplications(userid)
  .subscribe(applications => {
            console.log('app ' + applications);
            const menuItems: MenuItem[] = [];
            menuItems.push(
                new MenuHeaderItem({
                    type: MenuItemType.Header,
                    title: 'Applications',
                    description: null
                }));
            Object.keys(applications).map(element => {
                menuItems.push(this.formatconversion(applications[element]));
            });
            this.menu = menuItems;
        });

console.log('app ' + applications) not coming.

Any help will be appreciated. Thanks in advance.

1
Your code seems valid in a sense that the code in the subscribe block should be called once the http get call is successfully resolved. Do you get any errors in the console?Fjut

1 Answers

0
votes

I think you have problem with your code instead of this

getApplications(userid: string): Observable<any> {
let data =this.httpclient.get(this._getAPIUrl('user/' + userid + '/applications'))
  .map((res: Response) => res.json())
  return data;

}

use this

getApplications(userid: string): Observable<any> {
this.httpclient.get(this._getAPIUrl('user/' + userid + '/applications'))
  .map((res: Response) => res.json())

}

There is no need of of returning.

res.json()

will return what ever the server response is.try this and let me know if its work :)