SeanBallentine
SeanBallentine

Reputation: 173

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.

Upvotes: 0

Views: 294

Answers (2)

shanabus
shanabus

Reputation: 13115

The title appears to be coming off of the volumeInfo object on the return collection. This worked for me:

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

Example:

https://jsbin.com/puyayamuwi/edit?html,output

Upvotes: 0

Cerbrus
Cerbrus

Reputation: 72857

The response data has title placed in a volumeInfo object.

Replace:

var title = list[i].title;

With:

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

Upvotes: 5

Related Questions