1

I built a service, that gets a specific ID from an API observable. The service is working, if i console.log(data) from the service class, but i can't get the data in my component.

See the console

Service:

getSpecificStory(storyId) {
    return this.getToken()
      .map(idToken => {
        let headers = new Headers();
        headers.set('user_token', idToken)
        return this.http
          .get(`${this.apiUrl}/stories/${storyId}`, { headers: headers })
          .subscribe((res: Response) => {
            const data = res.json();
            console.log(data)
            return data;
          });
      })
      .catch(this.handleError);
  }

Component:

export class StoryModalComponent implements OnInit {
  story: any;
  storyId: any;
  hitsArray: Array<Object>;

  constructor(private storiesService: StoriesService, private route: ActivatedRoute) {
  }

  ngOnInit() {
    this.route.params
      .subscribe(
      params => {
        this.storyId = params['storyId']
      })
    console.log(this.storyId)
    this.getStoryObject();
  }


  getStoryObject() {
    console.log(this.storyId)
    this.storiesService.getSpecificStory(this.storyId)
      .subscribe(
      (data) => {
        this.story = data;
        console.log(this.story)
      })
  }
}
2
  • how does your component look? Commented Oct 16, 2017 at 13:03
  • Like i've posted? You mean the HTML? By the way, don't mind the "getToken()" function - it's just for fetching the id_token of the user in the firebase backend. Commented Oct 16, 2017 at 13:07

2 Answers 2

1

You are looking for the flatMap operator instead of map.

getSpecificStory(storyId) {
    return this.getToken()
        .flatMap(idToken => {
            let headers = new Headers();
            headers.set('user_token', idToken)
            return this.http
                .get(`${this.apiUrl}/stories/${storyId}`, { headers: headers })
        });
    })
}

.flatMap expects you to return an observable (in your case the this.http.get(...)). Now the getSpecificStory method is returning an observable. So you an subscribe in your component

this.storiesService.getSpecificStory(this.storyId)
    .subscribe(
        (data) => {
            this.story = data;
            console.log(this.story)
         })

This is the common method when you are chaining dependent observables (your firebase getToken() method and your this.http.get())

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

1 Comment

Yeah mate, that did the trick! Im still learning the RxJs lib., so thank you very much for this!
1

You have to return observable in order to user subscribe method in comonent.

getSpecificStory(storyId) {
return this.getToken()
  .map(idToken => {
    let headers = new Headers();
    headers.set('user_token', idToken)
    return this.http
      .get(`${this.apiUrl}/stories/${storyId}`, { headers: headers })
      .map((res: Response) => res.json());
      .catch(this.handleError);
  })
  .catch(this.handleError);}

1 Comment

I don't see the difference from mine, except you're using "map"? If you look at the service, i am returning the data with "return data;"

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.