Google Books API を使用して書籍のリストを取得していますが、一部の書籍エントリにAuthorsなどのキー/プロパティがない場合や、 Thumbnailがない場合があります。したがって、JavaScript は、アクセスしようとしているプロパティが未定義であり、アプリケーションが動かなくなったと言っています。
例キーワーク Java を含む書籍検索からの Json データの例
フルリンク
https://www.googleapis.com/books/v1/volumes?q=java&callback=jQuery191020691258599981666_1377508821982&_=1377508821983
例: Authorsが欠落している場合
TypeError: row.volumeInfo.authors is undefined
私は提案された2つの解決策を試しました
if ( typeof (authors) != "undefined"){}
と
if('authors' in booksObject) {}
しかし、このプロパティが存在する場合でもループに入らないため、それらのどれも機能していないようです。
これは私が呼び出す場所です
function populateListview(books) {
//iterate each returned item
$.each(books.items, function(i, row) {
//populate each row in the list
var htmlString = '<li><a href="book_details.html?id=' + row.id + '"><img src="';
htmlString += row.volumeInfo.imageLinks.thumbnail + '"';
htmlString += 'class="ui-li-has-thumb"/><h3>';
//Check if authors exists in JSON
htmlString += row.volumeInfo.title + '</h3><p>' + row.volumeInfo.authors[0] + '</p></a></li>';
//If not add an undefined authors string in the authors
console.log(htmlString);
//append new html to the list
$('#book_list').append(htmlString);
});
$('#book_list').listview('refresh');
// refresh the list-view so new elements are added to the DOM
};