2021-08-28

Google books API: "Cannot read property 'thumbnail' of undefined" on specific search

So basically i was following a Tutorial to search and list books using the google API. at the end an arrow function is added to add a default image to the books that dont have one.

cleanData = (data) => {
const cleanedData = data.body.items.map((book) => {
    if (book.volumeInfo.hasOwnProperty("publishedDate") === false) {
        book.volumeInfo["publishedDate"] = "0000";
    } else if (book.volumeInfo.hasOwnProperty("imageLinks") === false) {
        book.volumeInfo["imageLinks"] = {
            thumbnail:
                "https://freepikpsd.com/media/2019/10/no-image-available-icon-png-8-Transparent-Images.png",
        };
    }
 
  return book;
});

the problem is this function seems to not always work.

Example:

When searching "stephen king" the book "todo el cine de stephen king" does not have imageLinks.thumbnail so its added with a link to the default image. as shown in this image

however, when searching "Dragon Ball" it doesnt work, the error "TypeError: Cannot read property 'thumbnail' of undefined" pops up because some of the items dont have a default image assigned.

Error and items

I dont understand why it always works sometimes since the data seems to have the same format in both scenarios.



from Recent Questions - Stack Overflow https://ift.tt/3sSLU6x
https://ift.tt/eA8V8J

No comments:

Post a Comment