0

Iam new to angular and Observables .My Rest API always return a custom response type

 export class CustomApiResponse {
      isSucess: boolean | undefined; //  denotes whether request was sucess
      statusCode: number | undefined; //  custom status codes from webapi 
      error: string | undefined;      // error message in case of error
      value: any;                 //data which may be array of object or single object
    }

I am having a generic service for all httprequest

export class HttpHelperService {
  BaseUrl:String="mydominurl/api";
  constructor( private httpclient :HttpClient) { }

   GetData(url:string) : Observable<CustomApiResponse>{
    return  this.httpclient.get<CustomApiResponse>(this.BaseUrl+url);
   }
  }

Now I need to call this httphelper and extract the data from the response as observable using my services like below (Which Iam not able to figure out)

export class CategoryService {
  url:String="/api_category";
  constructor( private httphelper :HttpHelperService) {  }

   getCategories(url:string) : Observable<Category[]>{   

     this.httphelper.GetData(url).subscribe(val=>{
      if(val.isSucess){ 
     return val.value; //How to do this
     }} );  }

}

Please advice me how to rewrite above method so that I can call the above services from my components like below

export class CategoryListComponent implements OnInit {

  Items: Observable< Category[]> | undefined;
  url:string="/Api_category";
  constructor( private catservice :CategoryService) {
  }

  ngOnInit(): void {
    this.catservice.getCategories(this.url).subscribe(val=> this.Items=val);
  }
}
2
  • Don’t subscribe in your service. Just have the service return the observable and subscribe in the component. Commented Nov 6, 2021 at 19:07
  • 1
    Oh.. and if you just want the value back, you can pipe the obs in your service like this.httphelper.GetData(url).pipe(map( (val: any)) => val.value )) Commented Nov 6, 2021 at 19:12

1 Answer 1

1

I think what you are trying to do is modify the value that is provided to the CategoryListComponent, after getting a reply from the HttpHelper.

This can be done using the map operator, like this:

export class CategoryService {
  url:String="/api_category";
  constructor( private httphelper :HttpHelperService) {  }

   getCategories(url:string) : Observable<Category[]>{   
     return this.httphelper.GetData(url).pipe(map((val) => val.isSuccess ? val.value : []));
   }

}

This will return a new observable that will change the value of the response from GetData to something else (in this case, val.value if the val.isSuccess was true, and otherwises an empty list).

Then your component is going to subscribe and it will work fine.

Always try to subscribe to observables as the very last step of a long chain of observables. It is going to require a lot of learning of RxJS but it is the proper way to do it, ideally you will have your component subscribe to the observable and the rest of intermediate services will be mapping, filtering, transforming or combining the information.

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

4 Comments

I tried this.Items= this.catservice.getCategories(this.url).subscribe(); in component but its giving below error ... sorry if made some dumb mistake ... Type 'Subscription' is missing the following properties from type 'Observable<Category[]>': _isScalar, source, operator, lift,
Also I tried the below also in component this.catservice.getCategories(this.url).subscribe( val=>this.Items=val);
But worked when i do this only this.Items= this.catservice.getCategories(this.url); in component
A subscription is not an observable. I suggest you to read through some tutorials, this should help you understand better what is going on instead of having to ask every time you find an issue. I highly recommend Learn RxJS portal, and you can find these two links very interesting: RxJS Primer, Get started transforming streams.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.