gpt4 book ai didi

javascript - topojson 未捕获类型错误 : Cannot read property 'feature' of undefined

转载 作者:行者123 更新时间:2023-11-29 19:07:59 24 4
gpt4 key购买 nike

背景

  • JavaScript 在调用 topojson.feature(topology, object) 时失败.这是 topojson-client 的文档,它看起来是在 topojson 升级到 2.x 时添加的,但它与我使用的版本 1.6.26 中的想法相同。
  • 使用 webpack 构建用于生产的 JS 和用于本地开发的 webpack-dev-server。
  • 直到最近它都运行良好。

问题

relevant_page.js:705 Uncaught TypeError: Cannot read property 'feature' of undefined

代码

package.json:

...
"dependencies": {
...
"topojson": "^1.6.26",

相关页面.js:

import topojson from topojson;
...
function ready(data) {
var topojsonStates = topojson.feature(us, us.objects.states).features;

疑难解答

我在浏览器中抛出错误的行设置了一个断点:

var topojsonStates = _topojson2.default.feature(us, us.objects.states).features;

并检查这些以查看发生了什么:

us: Object
...
type: "Topology"

us.objects.states: Object
...
type: "Topology"

_topojson2.default: undefined

_topojson2.feature(us, us.objects.states).features: Array[53]

_topojson2.default 未定义(并且 _topojson2.feature 有效)这一事实让我停顿了一下,但我不确定那里发生了什么。有什么想法吗?

笔记/更新

这与 how to consume an npm package with ES6 module via webpack 不同,因为如果您必须继续处理的主要问题是 Uncaught TypeError,则无法解决该问题和答案。让我知道是否有任何我应该添加到问题或答案中以使其更有用和更易于搜索的内容。

最佳答案

遇到一个GitHub issue有人报告了同样的事情,已在 pull request 中修复答案是将 topojson 作为命名空间导入:

import * as topojson from 'topojson';

topojson-client documentation 是怎样的指示您导入它。我没有意识到 1.6.x 会是一样的。听起来这在切换到 ES6 后一定是坏了。

关于javascript - topojson 未捕获类型错误 : Cannot read property 'feature' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41291142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com