cytoscape js dijkstra 方法在回调中抛出 TypeError
cytoscape js dijkstra method throwing TypeError on the callback
当我 运行 以下代码时出现以下错误:
TypeError: Cannot read property 'data' of undefined
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
Arrow functions 没有 this 绑定。但是边缘被传递给那个权重函数,如果您将代码更改为:
可能会更好读
const dijkstra = testGraph.elements().dijkstra('#a', (edge) => {
return edge.data('weight');
}, false);
var distanceToC = dijkstra.distanceTo(testGraph.$('#c')); // 3
当我 运行 以下代码时出现以下错误:
TypeError: Cannot read property 'data' of undefined
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
Arrow functions 没有 this 绑定。但是边缘被传递给那个权重函数,如果您将代码更改为:
可能会更好读const dijkstra = testGraph.elements().dijkstra('#a', (edge) => {
return edge.data('weight');
}, false);
var distanceToC = dijkstra.distanceTo(testGraph.$('#c')); // 3