【问题标题】:cytoscape js dijkstra method throwing TypeError on the callbackcytoscape js dijkstra方法在回调中抛出TypeError
【发布时间】:2017-10-31 01:03:18
【问题描述】:

运行以下代码时出现以下错误:

TypeError: 无法读取未定义的属性“数据”

const elementsList = [
            {data: {id: 'a'}},
            {data: {id: 'b'}},
            {data: {id: 'c'}},
            {data: {id: 'ab', source: 'a', target: 'b', weight: 4}},
            {data: {id: 'as', source: 'a', target: 'c', weight: 3}}
        ];
        const testGraph = cytoscape({elements: elementsList});
        const dijkstra = testGraph.elements().dijkstra('#a', () => {
            return this.data('weight');
        }, false);

这与 cytoscape 文档中描述的代码几乎完全相同,所以我不确定我哪里出错了。 当我在没有回调的情况下运行 dijkstra 时,我没有收到任何错误,我什至能够对返回的结果执行 pathTo()distanceTo()FIDDLE

【问题讨论】:

    标签: javascript graph-theory dijkstra cytoscape


    【解决方案1】:

    Arrow functions 没有 this 绑定。但是边缘会传递给该权重函数,如果您将代码更改为:

    const dijkstra = testGraph.elements().dijkstra('#a', (edge) => {
      return edge.data('weight');
    }, false);
    
    var distanceToC = dijkstra.distanceTo(testGraph.$('#c')); // 3
    

    Fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 2016-05-05
      • 2010-12-15
      • 1970-01-01
      相关资源
      最近更新 更多