5

I have this project I am working on. the idea is to retrieve books from google book API, using angular 4

I am struggling to understand how to read the JSON response, I am still learning Angular. I was searching the internet and found this source code on GitHub google bookstore

I am getting the following error

Argument of type 'Response' is not assignable to parameter of type 'string'.

for this line

  let bookResponse = JSON.parse(body);

I am not sure if I am doing it the correct way. appreciate your help

below is my method for send HTTP request.

  getBooks(searchparam: any){
let par = new HttpParams();
par.set('q', searchparam);

return this.httpClient.get('https://www.googleapis.com/books/v1/volumes', {
  params : new HttpParams().set('q',searchparam)
}).map(
  (response: Response) => {
    let data = response;

    return data;
  }
);

}

below is the method to get data from HTTP request and read JSON response

  getBook() {

const dataArrya = this.searchForm.get('param').value;

console.log(dataArrya);
this.requestService.getBooks(dataArrya)
  .subscribe(
    response  => {
      this.printData(response)
    //  console.log(this.bookData);
    },
    (error) => console.log(error)
  ); 

}

 private printData(res: Response) {

let body = res;
let books: Array<Book> = new Array<Book>();
let bookResponse = JSON.parse(body);
console.log(bookResponse);
console.dir(bookResponse);
for (let book of bookResponse.items) {
  books.push({
    title:book.volumeInfo.title,
    subTitle: book.volumeInfo.subTitle,
    categories: book.volumeInfo.categories,
    authors: book.volumeInfo.authors,
    rating: book.volumeInfo.rating,
    pageCount: book.volumeInfo.pageCount,
    image: book.volumeInfo.imageLinks === undefined ? '' : book.volumeInfo.imageLinks.thumbnail,
    description: book.volumeInfo.description,
    isbn: book.volumeInfo.industryIdentifiers,
    previewLink: book.volumeInfo.previewLink
  });
}

}
OceanWavez
  • 291
  • 1
  • 2
  • 10
  • 1
    By the way, there's an error in the code above. HttpParams instances are immutable, and `.set` should be chained https://stackoverflow.com/questions/45210406/angular-4-3-httpclient-set-params . This `par.set('q', searchparam)` won't work. – Estus Flask Oct 29 '17 at 07:57
  • Thanks Estus, it worked for me because i have only one parameter, but if i have more, then it is a problem. – OceanWavez Oct 29 '17 at 12:43
  • It works because you have correct code in another place, `params : new HttpParams().set('q',searchparam)` – Estus Flask Oct 29 '17 at 14:51

2 Answers2

11

JSON.parse takes a string, but you're passing it an Angular Response, which is an object (not a string). In order to convert an Angular Response to a string, you can call toString on it, like this:

let bookResponse = JSON.parse(body.toString());
Michael Hulet
  • 3,253
  • 1
  • 16
  • 33
4

As the reference states,

The responseType value determines how a successful response body will be parsed. If responseType is the default json, a type interface for the resulting object may be passed as a type parameter to request().

HttpClient get already parses the response with JSON.parse by default and there is no need to call it twice.

The result is plain object, not Response (it belongs to Http API and can be confused with Response global when not being imported).

The mentioned repository uses Angular 2 and Http, and the code from there isn't suitable here. HttpClient is newer API that was introduced in Angular 4. The main practical difference between Http and HttpClient is that the latter doesn't require to add .map(response: Response => response.json()) to the request.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • Please help me here https://stackoverflow.com/questions/57214946/argument-of-type-object-is-not-assignable-to-parameter-of-type-httpresponsea – Hasani Jul 26 '19 at 09:07