0

I have a api call to categories and everything works fine. Now if i need categories in another page its making a call again whereas i need to store it locally. I just followed the approach i used to practise in angular1 but its throwing error. The commented lines are my trials.

private categories: any;
  constructor(private http: Http) { }

  getCategories(): Promise<any> {
    // if(!this.categories){
    return this.http.get(environment.API_ENDPOINT + 'categories')
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
    // }else{
    //   console.log("call blocked");
    //   return Promise.resolve(this.categories);
    // }

  }


  private extractData(res: Response) {
    let body = res.json();
    //  this.categories = body || [];
    return body;
    //return this.categories;
  }

  private handleError(error: any): Promise<any> {

    return Promise.reject(JSON.parse(error.message || error._body || error));
  }

How to store the api received data so i call the service, it will check for the previous data, and if not available, it will fetch data from server.

Thanks in advance.

Trying with observable and of

import { Injectable } from '@angular/core';

import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of'; //updating import library, which threw error
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/publishReplay'
import {Cities} from '../interface/cities';
import { environment } from '../../environments/environment';
@Injectable()
export class CityService {
  private endPoint = environment.API_URL+'cities';
  private data:any;
  constructor(
    private http: Http) { }

   getAll (): Observable<Cities[]> {
     if(this.data){
        return Observable.of(this.data); // 
      }
    return this.http.get(this.endPoint)
                    .map(this.extractData)
                    .catch(this.handleError);
  }
  private extractData(res: Response) {
    let body = res.json();
    this.data = body || body.data || { };
    return this.data;
  }
  private handleError (error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

}

Tried with publish count

import { Injectable } from '@angular/core';

import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/publishReplay'
import {Cities} from '../interface/cities';
import { environment } from '../../environments/environment';
@Injectable()
export class CityService {
  private endPoint = environment.API_URL+'cities';
  private data:any;
  constructor(
    private http: Http) { }

   getAll (): Observable<Cities[]> {
    return this.http.get(this.endPoint)
                    .map(this.extractData)
                    .publishReplay(1) // does not stop duplicate calls
                    .refCount() // does not stop duplicate calls
                    .catch(this.handleError);
  }
  private extractData(res: Response) {
    let body = res.json();
    this.data = body || body.data || { };
    return this.data;
  }
  private handleError (error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

}
4
  • 3
    stackoverflow.com/questions/36271899/… Commented Mar 9, 2017 at 11:16
  • @GünterZöchbauer Can you please take a look into my update and advise where i am wrong Commented Mar 12, 2017 at 10:04
  • import 'rxjs/add/observable/of'; was the mistake. I copy pasted from an answer in stackoverflow which missed add. Not sure whether it was on previous version without add Commented Mar 12, 2017 at 21:39
  • upon putting console.log(this.data) inside getAll() function, i always get undefined. Which i assume, the service is reinitiated. How to avoid it. Commented Mar 12, 2017 at 22:30

1 Answer 1

1

Atlast after 2 days of struggle i found only this as a working solution to me.

Though i posted a question with cities, this is with categories

import { Injectable } from '@angular/core';

import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
import { Cities } from '../interface/cities';
import { environment } from '../../environments/environment';
import { LocalStorageService } from 'angular-2-local-storage';
@Injectable()
export class CityService {
  private endPoint = environment.API_URL + 'cities';
  private data: any;
  constructor(
    private http: Http,
    private localStorageService: LocalStorageService) { }

  getAll(): Observable<Cities[]> {
    if (this.localStorageService.get('cities')) {
      return Observable.of(this.localStorageService.get('cities'));
    } else {
      return this.http.get(this.endPoint)
        .map(this.extractData)
        .do((data) => this.localStorageService.set("cities", data))
        .catch(this.handleError);
    }
  }
  private extractData(res: Response) {
    let body = res.json();
    this.data = body || body.data || {};

    //this.localStorage.setItems$.subscribe("cities", "Hello world");
    return this.data;
  }
  private handleError(error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure
    let errMsg: string;
    if (error instanceof Response) {
      const body = error.json() || '';
      const err = body.error || JSON.stringify(body);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
    return Observable.throw(errMsg);
  }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.