简体   繁体   中英

Error while using body.json() for parsing response from http.get()

Tried using body.json() to assign data to an array of object but as it returns a promise tried this. But browser throws error telling me json() is not a function.

getRecipes() {
  this.http.get('https://recipe-book-1be52.firebaseio.com/recipes.json').subscribe(
    (response: Response) => {
      response.json().then(
        (data) => {
          this.recServ.setRecipes(data)
        }
      );
    }
  )
}

angular httpClient already does the.json() for you

Below code snippet may help you

  getRecipes() {
    this.http.get('https://recipe-book-1be52.firebaseio.com/recipes.json').subscribe(
      (response: Response) => {
            this.recServ.setRecipes(JSON.parse(JSON.stringify(response)));
      }
    )}

You can actually replace it like this, Also, can assign response to an interface to strict-type it.

getRecipes() {
  this.http.get('https://recipe-book-1be52.firebaseio.com/recipes.json').subscribe(
    (response) => this.recServ.setRecipes(response)
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM