1

I am trying to get image thumbnail in my XMLHttprequest but when I try I get an error "Uncaught TypeError: Cannot read property 'thumbnail' of undefined at XMLHttpRequest.xhr.onload (app.js:73)" Same issue for infoLinks: Request

Could anyone help? Thank you. here is my code:

const btnRechercher = document.getElementById('btn-rechercher');

let bookSearch = document.querySelector('.modal-content').value; 
const listElement = document.querySelector('.list-books'); 
const template = document.getElementById('book-template');

function searchBookHandler () {

let title = document.getElementById('titre-livre').value;
let author = document.getElementById('auteur').value;
let search = title + author;

console.log(search);

const xhr = new XMLHttpRequest();


xhr.open('GET', "https://www.googleapis.com/books/v1/volumes?q=" + search);

xhr.responseType = 'json';

xhr.onload = function() {

const listOfBooks = xhr.response;
console.log(listOfBooks);

for (i=0; i < listOfBooks.items.length; i++) {

const postEl = document.importNode(template.content, true);
postEl.querySelector('.id').textContent = listOfBooks.items[i].id;
postEl.querySelector('.titre').textContent = listOfBooks.items[i].volumeInfo.title;
postEl.querySelector('.author').textContent = listOfBooks.items[i].volumeInfo.authors;
postEl.querySelector('img').textContent = listOfBooks.items[i].volumeInfo.imageLinks.thumbnail;

listElement.append(postEl);

  }

};

xhr.send();

}

btnRechercher.addEventListener('click', searchBookHandler, false);

  • have you tried to run the search in the address bar to see what you get? the URL will be in the f12 debugger, under networking - i suspect your query is wrong as you are gluing your params together without a separator: `let search = title + author;` so i would compare URLS from your code - with one that works in address bar `https://www.googleapis.com/books/v1/volumes?q=.............` – developer Apr 15 '20 at 12:13
  • I think you should search stack-overflow more before posting, see: https://stackoverflow.com/questions/7908954/google-books-api-searching-by-isbn – developer Apr 15 '20 at 12:25
  • Yes, Basically my request works fine for the id, title and author but not for the image. – Hermann Ouré Apr 15 '20 at 13:06

0 Answers0