Fetching and combine data from Api In Angular 6

Qusay Saad

How to retrieve data from API and For each user id returns his posts with their comments in one JSON object?

posts can be fetched from this API: https://jsonplaceholder.typicode.com/posts

and their comments from this API: https://jsonplaceholder.typicode.com/comments

//Get User Posts And Comments
getUser() {
    this.http.get('https://jsonplaceholder.typicode.com/posts') && this.http.get('https://jsonplaceholder.typicode.com/comments')

    .subscribe(data => {
      this.posts = data;
    });

  }
Sachila Ranawaka

use forkJoin to combine requests

let req1 = this.http.get('https://jsonplaceholder.typicode.com/posts')  
let req2 = this.http.get('https://jsonplaceholder.typicode.com/comments')

forkJoin([req1,req2 ] )
   .subscribe(data => {
      this.posts = data;
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related