Angular : Types with http client

Angular : Types with http client

introduction

here in the http request , we will define what type of data we are expecting.

  • in the previous post we have created one Post model and here we are using it will the post request.
private fetchPosts() {

    this.http.get< { [key:string]:Post  } >('https://ngfirstproject-default-rtdb.asia-southeast1.firebasedatabase.app/posts.json')
    .pipe( 
      map(responseData => {

      const postArray=[];

      for (const key in responseData) {
        if(responseData.hasOwnProperty(key)) {
        postArray.push( { ...responseData[key] , id:key} )
        }
      }
      return postArray;
    })

    )
    .subscribe(
      posts => {
        console.log(posts[2]);
      }
    )
  }
}