【问题标题】:"Cannot read property "0" from undefined" for Google Books APIGoogle Books API 的“无法从未定义中读取属性“0””
【发布时间】:2019-07-26 16:49:20
【问题描述】:

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

未捕获的类型错误:无法读取未定义的属性“0”

这是指以下代码行:

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

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

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

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

/*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"

    })

}

【问题讨论】:

  • console.log(data.items[i].volumeInfo.industryIdentifiers) 的结果是什么?
  • 当你得到错误时,你能在volumeInfo属性下显示JSON字符串,而不是当你没有得到错误时?
  • console.log 的结果是未定义的。但即使有像 if ( data.items[i].volumeInfo.industryIdentifiers !="undefined"){ isbn = data.items[i].volumeInfo.industryIdentifiers[0].identifier; 这样的解决方案} 我仍然收到错误消息。
  • 当 console.log(data.items[i].volumeInfo.industryIdentifiers) 正常工作时,我收到 0: identifier: "9781781106945" type: "ISBN_13" proto:对象 1:标识符:“1781106940”类型:“ISBN_10”

标签: javascript api typeerror


【解决方案1】:

不查看发生错误时收到的实际数据与没有错误时收到的实际数据,以下是防止异常发生的方法:

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”不为空,并且其中包含第零个元素。您可能在尝试时没有检查第二部分。

如果即使这个解决方案不起作用,请添加您在错误时和成功时得到的实际响应。

【讨论】:

  • 检查 data.items[i] 的存在和其他语句似乎有效。但是现在当我打印“isbn”时,如果它因为不存在而没有定义,我会看到错误 Uncaught ReferenceError: isbn is not defined 为了解决这个问题,我应该使用类似的语句
  • @EdoardoTrotta 我没有意识到你还没有初始化 isbn 变量。您应该在顶部放置一条语句来初始化所有变量。类似var isbn = ""
猜你喜欢
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多