calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
734 stars 229 forks source link

TypeError: Right-hand side of 'instanceof' is not an object #201

Open simplexag opened 11 months ago

simplexag commented 11 months ago

I keep getting the following error

shp.js?t=1699109482459:14409 Uncaught (in promise) TypeError: Right-hand side of 'instanceof' is not an object at isArrayBuffer (shp.js?t=1699109482459:14409:22) at toBuffer (shp.js?t=1699109482459:14397:11) at shp.parseShp (shp.js?t=1699109482459:14538:13)

Using Vue 3

 methods: {
    async openZip() {  
    var reader = new FileReader();
      reader.readAsArrayBuffer(this.file[0]);
      reader.onload = () => {
        this.parseShape(reader.result);
      }
    },
    async parseShape(data) {
        shp.parseShp(data).then(geojson => {
          console.log(geojson); 
        })

    },
  }

Thank you for any help!

pChao3 commented 10 months ago

+1

kasenZrbl commented 10 months ago

这个是index.js文件里有一处检验代码出错了,是之前设置global对象时留下的问题 function isArrayBuffer (subject) { return subject instanceof global.ArrayBuffer || Object.prototype.toString.call(subject) === '[object ArrayBuffer]'; } 这个函数会检验传输的是否为二进制流,里面的逻辑判断使用了global这个全局变量,我看之前给的解决方案是在config文件内define了一个空的global全局变量,但是ArrayBuffer这个变量只有在window里面有,所有只需要在使用shpjs库函数前,执行一下 global = window ; 让global拥有window的属性方法即可

sqlAIJava commented 7 months ago

如果是vite项目,在vite.config.ts中加入global: 'window' export default defineConfig({ define: { global: 'window' }, })