echarts bug:Invalid geoJson formatCannot read properties of undefined (reading ‘length‘)

​​​​​​​

  • 问题以及原因:最近写地图遇到这么一个bug,由于数据是自己生成的,根据地图我们了解到一些区域并不是连在一起的但是是属于同一个镇/街道,从而有了如下报错

        -- Invalid geoJson formatCannot read properties of undefined (reading ‘length‘)

  • 解释:(小编在4.9.0的echarts下做的)由于生成的地图里属于同一个区域是两块不连续的地图块组成的,但是不是连续的,从而数据也不是在同一个数组中,如下图:

  •  echarts 中对于此类型没有做处理,如下图源码位置以及源码:

echarts版本:4.9.0

-- 源码位置:echarts/lib/coord/geo/parseFeoJson

-- 源码内容 

  • 解决方法(一): 将该代码内容赋值入echarts源码中,注释原来的方法
function _default(geoJson, nameProperty) {
    decode(geoJson);
    return zrUtil.map(
        zrUtil.filter(geoJson.features, function(featureObj) {
            if (featureObj.geometry.geometries) {
                let geometry = featureObj.geometry.geometries.map(i => {
                    return i.coordinates;
                });
                let { type, properties, ...params } = featureObj;
                return { type, properties, geometry };
            }
            // Output of mapshaper may have geometry null
            return (
                featureObj.geometry &&
                featureObj.properties &&
                featureObj.geometry.coordinates &&
                featureObj.geometry.coordinates.length > 0
            );
        }),
        function(featureObj) {
            var properties = featureObj.properties;
            var geo = featureObj.geometry;
            var coordinates = geo.coordinates;
            var geometries = [];
            if (geo.type === "GeometryCollection") {
                let geometry = {
                    type: "Polygon"
                };
                let coordinatesArr = featureObj.geometry.geometries.map(i => {
                    return i.coordinates;
                });
                geometry.coordinates = coordinatesArr;
                coordinatesArr.forEach(i => {
                    geometries.push({
                        type: "polygon",
                        // According to the GeoJSON specification.
                        // First must be exterior, and the rest are all interior(holes).
                        exterior: i[0],
                        interiors: i.slice(1)
                    });
                });
            }
            if (geo.type === "Polygon") {
                geometries.push({
                    type: "polygon",
                    // According to the GeoJSON specification.
                    // First must be exterior, and the rest are all interior(holes).
                    exterior: coordinates[0],
                    interiors: coordinates.slice(1)
                });
            }
            if (geo.type === "MultiPolygon") {
                zrUtil.each(coordinates, function(item) {
                    if (item[0]) {
                        geometries.push({
                            type: "polygon",
                            exterior: item[0],
                            interiors: item.slice(1)
                        });
                    }
                });
            }
            var region = new Region(
                properties[nameProperty || "name"],
                geometries,
                properties.cp
            );
            region.properties = properties;
            return region;
        }
    );
}
  • 缺点:由于echarts是一个公共的开源的工具,如果我们对工具进行了修改,将来别人用这个项目,或者下一任前端要维护这个项目时很容易留下一个坑,要么提前对接好,说这里改了开源组件,你跑代码之前也需要改,但是如果人使用的人多呢,会留下维护不易
  • 解决方法(二):手动修改获取的数据,将属于一个县、街道的多个区域块数据重新整合(虽然麻烦点,但至少不会留下坑)

 -- 例子:原数据格式

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "GeometryCollection",
        "geometries": [
          {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  119.92127816945431,
                  34.326185806177186
                ],
                [
                  119.89337116971991,
                  34.33111868503994
                ],
                ……数据
              ]
            ]
          },
          {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  120.09353416939943,
                  34.28201088958558
                ],
                [
                  120.10072916997201,
                  34.280715921436695
                ],
                ……数据
              ]
            ]
          }
        ]
      },
      "properties": {
        "name": "滨淮农场",
        "fill-opacity": 0,
        "stroke-opacity": 1,
        "stroke": "#ff0000"
      }
    }
  ]
}

修改后数据格式:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "GeometryCollection",
        "geometries": [
          {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  119.92127816945431,
                  34.326185806177186
                ],
                [
                  119.89337116971991,
                  34.33111868503994
                ],
                ……数据
              ],
              [
                [
                  120.09353416939943,
                  34.28201088958558
                ],
                [
                  120.10072916997201,
                  34.280715921436695
                ],
                ……数据
              ]
            ]
          }
        ]
      },
      "properties": {
        "name": "滨淮农场",
        "fill-opacity": 0,
        "stroke-opacity": 1,
        "stroke": "#ff0000"
      }
    }
  ]
}

### 解决开发服务器启动时出现的 `TypeError` 问题 当开发者尝试运行开发服务器并遇到类似于 `TypeError: Cannot read properties of undefined (reading 'includes')` 的错误时,这通常表明某个对象未被正确定义或初始化就试图访问其属性或方法。以下是可能的原因以及解决方案: #### 可能原因分析 1. **变量未定义** 如果代码中存在对未声明或未赋值的对象调用了 `.includes()` 方法,则会触发此错误。例如,在 JavaScript 中,如果某变量为 `undefined` 或 `null` 并对其执行 `.includes()` 操作,就会抛出该异常[^1]。 2. **依赖版本不匹配** 开发环境中使用的某些库可能存在兼容性问题。比如,较新的 Webpack 配置文件可能包含了旧版 Node.js 不支持的方法或语法结构[^2]。 3. **配置文件中的错误** 很多情况下,这种类型的错误来源于项目构建工具(如 Webpack、Vite 等)的配置不当。特别是当这些配置引用了一个不存在或者尚未加载完成的数据源时,容易引发上述错误消息[^3]。 #### 排查与修复建议 ##### 修改代码逻辑以处理潜在的 null/undefined 值 确保任何可能会成为参数传递给`.includes()`函数前已被适当验证不是null也不是undefined状态。 ```javascript if (myVariable && typeof myVariable === "string") { console.log(myVariable.includes('test')); } else { console.error("Invalid input"); } ``` ##### 更新Node.js环境至最新稳定版本 由于现代JavaScript特性(像箭头函数,解构分配等)需要更高版本的支持才能正常工作;因此确认当前系统的nodejs是否满足最低需求是非常重要的一步.[^4] ##### 审核第三方模块及其文档说明 仔细查阅所引入插件官方仓库里关于安装指南部分是否有特别指出针对不同操作系统下额外步骤的要求;同时留意issues区是否存在相似反馈记录以便快速定位具体症结所在位置.[^5] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值