Google图书API的“无法从未定义读取属性“ 0””

时间:2019-07-26 08:28:49

标签: javascript api typeerror

在我的script.js的Google Book API搜索网络应用中,很少遇到以下错误:

  

未捕获的TypeError:无法读取未定义的属性'0'

这是指以下代码行:

isbn = data.items[i].volumeInfo.industryIdentifiers[0].identifier;

那么,有没有办法避免此错误,并在分配变量之前检查查询返回的数据中是否存在industryIdentifiers

我尝试先检查industryIdentifiers是否存在,然后将值分配给变量,并在分配变量之前检查industryIdentifiers是否为null或“ undefined”,但是我无法解决此问题。

这是我目前正在使用的代码:

/*global document, $, console, event, results */

function bookSearch() {                          //function triggered by button
    /* eslint-disable no-console */
    event.preventDefault();                      //preventing auto refresh of the page
    //console.log("This function is working.");  //console.log to see if the function was running properly

    var search = document.getElementById('book').value; //storing the value of search
    document.getElementById('results').innerHTML = '';  
    //console.log(search);                       //console.log to see "search" in console 

    $.ajax({

        url: "https://www.googleapis.com/books/v1/volumes?q=" + search + "&startIndex=0&maxResults=40&key=AIzaSyCOBbymaad4eBVNFVF5JC-Pc0TQzE6AHOw", //linking API + our search
        //-------------^this increments the number of results returned. default 10
        dataType: "json",

        success: function(data) {
            console.log(data)
            //var results = document.getElementById('results');  //creating variable results

            //var title,authors, publisher,link,thumbNail,isbn = ''; //variables to use in results.innerHTML

            for (i = 0 ; i < data.items.length ; i++ ) {
                         //-------------^loop runs as much as the lenght of items
                title = data.items[i].volumeInfo.title;              //storing informations to display
                authors = data.items[i].volumeInfo.authors;
                link = data.items[i].volumeInfo.infoLink;
                thumbNail = data.items[i].volumeInfo.imageLinks.smallThumbnail;

                if( typeof data.items[i].volumeInfo.publisher != "undefined"){
                    publisher = data.items[i].volumeInfo.publisher;

                }

                isbn = data.items[i].volumeInfo.industryIdentifiers[0].identifier;

                results.innerHTML += "<h2>" + title + "</h2>" +      //writing the results returned 
                                     "<h3>By: " + authors + "</h3>" 
                if(publisher != null){                               //this solves the problem of showing 

                    results.innerHTML += "<h3>Published by: " + publisher + "</h3>"   

                 }  
                 results.innerHTML += "<a href=" + link +'"><img src="'+ thumbNail + '"></a>' +
                 "<h4>ISBN " + isbn + "</h4>" +    
                 "<hr><br>"



            }  
        },

    type: "GET"

    })

}

1 个答案:

答案 0 :(得分:0)

在不检查有错误与无错误的情况下收到的实际数据的情况下,可以通过以下方法防止异常发生:

if (data.items[i].volumeInfo.industryIdentifiers 
        && data.items[i].volumeInfo.industryIdentifiers[0]) {
    isbn = data.items[i].volumeInfo.industryIdentifiers[0].identifier;
}

如果不确定volumeInfo属性,甚至可以添加要检查的属性:

if (data.items[i].volumeInfo 
        && data.items[i].volumeInfo.industryIdentifiers 
        && data.items[i].volumeInfo.industryIdentifiers[0]) {
    isbn = data.items[i].volumeInfo.industryIdentifiers[0].identifier;
}

这基本上检查“ industryIdentifiers”是否不为空,并且其中是否包含第零个元素。您尝试时可能会错过第二部分的检查。

请添加您在错误发生时以及成功时获得的实际响应,即使该解决方案也不起作用。