0

I'm getting the error "Uncaught TypeError: Cannot read property 'thumbnail' of undefined" and I don't know how to fix it. I'm using Google Book API and I'm getting this error. I don't understand this because before I use thumbnail I checked if this element exists and still getting this error.

JS:

      if(Ui.checkIfExist(response.items[i].volumeInfo.imageLinks.thumbnail) && 
      response.items[i].volumeInfo.imageLinks.thumbnail.length>0){
    var img = document.createElement('div');
    img.classList.add('img-book');
    responseItem.appendChild(img);
    img.innerHTML = "<img src=" + response.items[i].volumeInfo.imageLinks.thumbnail+">"+"</img>";
   }
  • What is the response you get? Seems like it doesn't have an `imageLinks` property. – VLAZ May 17 '21 at 09:53
  • You're probably getting the error from inside the if statement, because you're trying to reach length property of something which is undefined. You should check for its existence before. – keune May 17 '21 at 10:38
  • @keune `Cannot read property 'thumbnail' of undefined"` means when trying to read `.thumbnail` as a property, the value that is attempted on is `undefined`, not the `.thumbnail` property itself. So with code like `response.items[i].volumeInfo.imageLinks.thumbnail` that means `response.items[i].volumeInfo.imageLinks` is `undefined`. – VLAZ May 17 '21 at 10:45
  • I checked response and thumbnail doesn't exist because it is undefinied. I change this to `if(Ui.checkIfExist(response.items[i].volumeInfo.imageLinks)` and I'm getting still errors but less –  May 17 '21 at 11:10

0 Answers0