未捕获的类型错误:无法读取属性'bounding_box'为null

Uncaught TypeError: Cannot read property 'bounding_box' of null

本文关键字:bounding null box 属性 读取 类型 错误      更新时间:2024-03-15

目前,我正在尝试从我搜索的关键词加载所有推文。实际上有10个结果,但我只看到3个结果,在我的控制台窗口中显示了一条错误消息。

这是错误消息:

Uncaught TypeError: Cannot read property 'bounding_box' of null

这是我的js代码:

(function(){
    var location = new Array();
    var query = '%23CM0655';
    var url = "search.php";
    $.post(url, {query:query}, function(tweets){
        console.log(tweets);
        $("#tweetResult tbody").html("");
        var geoEnabled = false;
        var placeName = "";
        var countryName = "";
        for(var i=0; i<tweets.statuses.length; i++){
            var row = $("<tr></tr>");
            var img = $("<td><div class='div_pic'><img src='" + tweets.statuses[i].user.profile_image_url + "' class='tweetpic'/>"+
                        "<label class='user_name' hidden>"+tweets.statuses[i].user.screen_name+"</label>"+
                        "<label class='divlatitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][0]+"</label>"+
                        "<label class='divlongtitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][1]+"</label>"+
                        "</div></td>");
            //var img = $("<td><div class='div_pic'><img src='" + tweets.statuses[i].user.profile_image_url + "' class='tweetpic'/></div></td>");
            row.append(img);
              // row.append($("<td></td>").html(tweets.statuses[i].user.screen_name));
            row.append($("<td></td>").html(tweets.statuses[i].user.screen_name));
            row.append($("<td></td>").html(tweets.statuses[i].text + "<br/>"));
            geoEnabled = tweets.statuses[i].user.geo_enabled;
            if(geoEnabled){
                placeName = tweets.statuses[i].place.name;
                countryName = tweets.statuses[i].place.country;
                if(placeName != null){
                    row.append($("<td></td>").html(placeName + "," + countryName + "<br/>"));
                }
                row.append($("<td></td>").html("Location: " + tweets.statuses[i].place.bounding_box.coordinates[0][0][0] + ", " + 
                    tweets.statuses[i].place.bounding_box.coordinates[0][0][1] + "<br/>"));
                row.append($("<td></td>").html(tweets.statuses[i].created_at));
            }
            $("#tweetResult tbody").append(row)
        }
    });
    setTimeout(arguments.callee, 60000);
})();

这是显示错误的行:

 "<label class='divlatitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][0]+"</label>"+

有什么方法可以解决这个错误吗。我想显示所有的推文,即使某些推文的位置没有启用。有人能帮我吗。非常感谢。

我通过在它周围添加一个if-else语句来解决它

if (tweets.statuses[i].user.geo_enabled && tweets.statuses[i].place != null) {
                var img = $("<td><div class='div_pic'><img src='" + tweets.statuses[i].user.profile_image_url + "' class='tweetpic'/>"+
                            "<label class='user_name' hidden>"+tweets.statuses[i].user.screen_name+"</label>"+
                            "<label class='divlatitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][0]+"</label>"+
                            "<label class='divlongtitude' hidden>"+tweets.statuses[i].place.bounding_box.coordinates[0][0][1]+"</label>"+
                            "</div></td>");
            }else{
                var img = $("<td><div class='div_pic'><img src='" + tweets.statuses[i].user.profile_image_url + "' class='tweetpic'/>"+
                    "<label class='user_name' hidden>"+tweets.statuses[i].user.screen_name+"</label>"+
                    "<label class='divlatitude' hidden>null</label>"+
                    "<label class='divlongtitude' hidden>"+tweets.statuses[i].user.location+"</label>"+
                    "</div></td>");
            }

将以下tweets.statuses[i].place.bounding_box.coordinates[0][0][0]更改为

tweets.statuses[i].place ? tweets.statuses[i].place.bounding_box.coordinates[0][0][0] : "No location data"

由于您在多个地方使用这个片段,我建议您将其存储在一个变量中并使用它

一般来说,如果你得到的数据对象可以有nullundefined,那么安全起见总是更好的。上面的代码可以像:

var location = (tweets.statuses[i].place && tweets.statuses[i].place.bounding_box && tweets.statuses[i].place.bounding_box.coordinates[0][0][0] ) || "No location";

假设,如果你有bounding_box,你肯定会有coordinates[0][0]