未捕获的类型错误:无法读取属性'bounding_box'为null
Uncaught TypeError: Cannot read property 'bounding_box' of null
目前,我正在尝试从我搜索的关键词加载所有推文。实际上有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"
由于您在多个地方使用这个片段,我建议您将其存储在一个变量中并使用它
一般来说,如果你得到的数据对象可以有null
或undefined
,那么安全起见总是更好的。上面的代码可以像:
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]
。
相关文章:
- 数组在递归方法中设置为null
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 无法获取属性'selectedIndex'的未定义引用或null引用
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 来自文本输入null的html javascript变量
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- php代码在textbox更改事件上显示null
- 相同的RegExp返回不同的结果-第一次是正确的结果,第二次是null
- 在php中提交数据时,如果某些值为null,而某些值为非null,如何进行查询
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- 为什么localStorage[“..”]未定义,而localStorage.getItem(“..”)为null
- ajax在输入等于null时进行检查
- html+javascript:如何在某些条件下禁用dropdownlist中的null选项
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- contentWindow.document.body is null
- 在经过DATE验证的列中创建null值
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- if(foo!==null)的计算结果为true,即使foo为null
- php函数的ajax html$_POST值返回null
- ASP Page_Load元素值为null