Argument of type 'Response' is not assignable to parameter of type 'string'
Asked Answered
C

2

6

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
  });
}

}
Casein answered 29/10, 2017 at 6:6 Comment(3)
By the way, there's an error in the code above. HttpParams instances are immutable, and .set should be chained #45210906 . This par.set('q', searchparam) won't work.Lipolysis
Thanks Estus, it worked for me because i have only one parameter, but if i have more, then it is a problem.Casein
It works because you have correct code in another place, params : new HttpParams().set('q',searchparam)Lipolysis
F
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());
Fury answered 29/10, 2017 at 6:14 Comment(1)
THANK YOU THANK YOUBattleplane
L
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.

Lipolysis answered 29/10, 2017 at 7:49 Comment(1)
Please help me here #57215446Hypaethral

© 2022 - 2025 — McMap. All rights reserved.