Search code examples
javascriptjsongoogle-books

Google Books API JSON object trouble


I am trying to pull from the Google Books API and insert the titles from the first 10 results into a web page. I have the site pulling the correct request and have the following callback function handling the results

function insert(books) {
    var list = books.items;
    var i;
    for(i = 0; i < 10; i++){
        var title = list[i].title;
    var tag = "result" + i;
    var x = document.getElementById(tag);
    x.innerHTML = title;
    }
}

For ease lets suppose the following call was made

<script src="https://www.googleapis.com/books/v1/volumes?q=Way of Kings&filter=partial&callback=insert"></script>

Right now it inserts the word "undefined" in every place it should insert a title. I can't find the error here.


Solution

  • The response data has title placed in a volumeInfo object.

    Replace:

    var title = list[i].title;
    

    With:

    var title = list[i].volumeInfo.title;