Javascript 未捕获类型错误:无法读取属性';国家';未定义的
这是我访问数据的方式:Javascript 未捕获类型错误:无法读取属性';国家';未定义的,javascript,geojson,topojson,Javascript,Geojson,Topojson,这是我访问数据的方式: $.getJSON("https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/meteorite-strike-data.json", function(data){ console.log(data); states=topojson.feature(data,data.objects.countries).features; } );
$.getJSON("https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/meteorite-strike-data.json",
function(data){
console.log(data);
states=topojson.feature(data,data.objects.countries).features;
}
);
但它给出了错误:uncaughttypeerror:无法读取未定义的属性“countries”
以下是我的数据示例:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
6.08333,
50.775
]
},
"properties": {
"mass": "21",
"name": "Aachen",
"reclong": "6.083330",
"geolocation_address": null,
"geolocation_zip": null,
"year": "1880-01-01T00:00:00.000",
"geolocation_state": null,
"fall": "Fell",
"id": "1",
"recclass": "L5",
"reclat": "50.775000",
"geolocation_city": null,
"nametype": "Valid"
}
},
该API调用返回的JSON上没有
对象
成员(也没有国家
成员):
您正在geojson文件上使用topojson.js。topojson包含一个名为objects的属性,该属性包含文件中的功能。topojson始终以开头(或至少具有“topology”类型): 您有geojson,geojson通常包含要素集合(即类型为“feature”的单个要素或类型为“polygon”、“point”等的几何图形): 由于geojson功能集合(或单个功能)没有对象属性,因此会出现错误,因为对象未定义 使用topojson时,
topojson.feature()
返回geojson功能集合,但由于您已经拥有geojson功能集合,如果您想访问这些功能,只需使用:
states = data.features;
注意:这个数据集是在另一个版本中出现的,geojson对于d3来说不是很有效,因为有点缺少坐标。在添加坐标之前,您需要过滤掉那些缺少坐标的点,请参见此和/或链接的问题。JSON响应是什么样子的?错误消息会告诉您出了什么问题。您正试图从
未定义的中获取.countries
,这意味着数据。对象是未定义的。什么是topojson.feature
?
{"type":"Topology",...
{
"type": "FeatureCollection",
"features": [ ... ]
}
states = data.features;