Mark
Mark

Reputation: 4950

Angular 9 - transform Response

Trying to learn how to transform http responses using pipes. The following function is called outside of that service:

public requestProjectList(assetId: number) {
    this.service.get(url)
    .pipe(map(data => {
      console.log(data);
    }));
  }

However I am not getting data.What am I doing wrong? Please explain.

Thanks

Upvotes: 0

Views: 1056

Answers (1)

bryan60
bryan60

Reputation: 29335

unlike promises, observables are "cold" until they're subscribed to, meaning the function and the transforms won't run until you subscribe.

public requestProjectList(assetId: number) {
    // return the observable
    return this.service.get(url)
    .pipe(map(data => {
      console.log(data);
      return data; // also return inside map prefer `tap` for simple logging
    }));
}

then call your function and subscribe to trigger:

this.service.requestProjectList(id).subscribe(result => console.log(result, "got it"))

Upvotes: 3

Related Questions