【问题标题】:Missing data in JSON from API call, gives error来自 API 调用的 JSON 中缺少数据,导致错误
【发布时间】:2022-01-15 11:44:32
【问题描述】:

我通过 API 调用服务器来获取数据

    const api_url = "https://my_urlxxx"

addMyData()
    .then(response => {
        //console.log('Working!');
    })
    .catch(error => console.log(error.message));
    
async function addMyData() {
    const response = await fetch(api_url);
    const data = await response.json();

然后我将 JSON 转换为 Leaflet.js 的 GeoJSON

geojson['type'] = 'FeatureCollection';
    geojson ['features'] = [];
    
    for (i = 0; i < data.length; i++) {
            x = data[i].location.coordinate.longitude;
            y = data[i].location.coordinate.latitude;
        console.log('get ', data[i].itemId); //find the error...
        console.log('get ', data[i].openingHours);
        console.log('get ', data[i].openingHours.services[0]);
        console.log('get ', data[i].openingHours.services[0].openDay); // error = undefined

        var newFeature = {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [x,y]
                },
                "properties": {
                    "status": data[i].status,
                    "itemId": data[i].itemId,
                    "name": data[i].name,
                    "openingMonday": days(data[i].openingHours.services[0].openDay) + ' ' + num2time(data[i].openingHours.services[0].openTime) + '\u2014' + num2time(data[i].openingHours.services[0].closeTime)
                  }
        }
        geojson ['features'].push(newFeature);
    }
    //console.log(JSON.stringify(geojson));
    loadData (geojson);
    };
    
    function loadData(MyGeoJSONData) {
    
    MyLayer = L.geoJson(MyGeoJSONData, {
    
    pointToLayer: function (feature, latlng) {
            if (feature.properties.status != "1") {
            return new L.shapeMarker(latlng, {
                radius: 4,
                color: '#252525', //#d53e4f
                fillOpacity: 0.3,
                weight: 2,
                shape: 'circle'
            })
            }
    },

问题是每个项目都没有“openingHours”的值。 这使得错误“未定义”,我无法检索数据以显示在 Leaflet 地图上。 如何在创建 GeoJSON 之前传递或忽略这些值,或者将它们过滤掉?

这是 openingHours 的 JSON 文件的一部分,JSON 列表中的某些项目可能会丢失整个部分,我无法通过这个...

   "openingHours": {
  "services": [
    {
      "closeDay": "Monday",
      "closeTime": "2359",
      "openDay": "Monday",
      "openTime": "0000"
    },
    {
      "closeDay": "Tuesday",
      "closeTime": "2359",
      "openDay": "Tuesday",
      "openTime": "0000"
    },
    {
      "closeDay": "Wednesday",
      "closeTime": "2359",
      "openDay": "Wednesday",
      "openTime": "0000"
    },
    {
      "closeDay": "Thursday",
      "closeTime": "2359",
      "openDay": "Thursday",
      "openTime": "0000"
    },
    {
      "closeDay": "Friday",
      "closeTime": "2359",
      "openDay": "Friday",
      "openTime": "0000"
    },
    {
      "closeDay": "Saturday",
      "closeTime": "2359",
      "openDay": "Saturday",
      "openTime": "0000"
    },
    {
      "closeDay": "Sunday",
      "closeTime": "2359",
      "openDay": "Sunday",
      "openTime": "0000"
    }
  ],
  "specialDates": [
    
  ]
},

【问题讨论】:

    标签: javascript json api leaflet


    【解决方案1】:

    你不需要在这里过滤,你可以检查三元组并返回一个空字符串(或任何你可能需要的),如果它不存在:

    "openingMonday": data[i]?.openingHours?  days(data[i].openingHours.services[0].openDay) + ' ' + num2time(data[i].openingHours.services[0].openTime) + '\u2014' + num2time(data[i].openingHours.services[0].closeTime): ''
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      相关资源
      最近更新 更多