在调试一个不相关的问题时,我的代码以打开浏览器调试器之前没有完成的方式崩溃。
TypeError: Cannot convert undefined or null to object
在移动mxCell
的事件处理程序中发生错误。
adjustPortPosition(sender, event)
{
...
for(var i = 0; i < event.properties.cells.length; i++)
{
var node = event.properties.cells[i];
var edges = this.graph.getEdges(node); // crashes here!
...
}
}
在调用mxGraph.getEdges
后发生崩溃。当我进入该调用时,该方法中的所有内容都没有问题地执行,并且它返回的值是3个mxCell
个对象的数组,而不是undefined
或null
。 getEdges
不会出现在堆栈跟踪中,而是出现在它所调用的行上。我所做的就是将返回值分配给变量,因此即使它是undefined
或null
,它也不应该进行任何类型的转换。
仅在移动具有子顶点的顶点时才会发生这种情况。我的图中没有子节点的任何顶点都不会产生错误。
为了使情况更加清晰,错误不会持续发生。在不更改代码的情况下,每次在调试时移动顶点时都会在崩溃之间切换多次,而根本无法重现崩溃。
这里是mxgraph.getEdges:
mxGraph.prototype.getEdges = function(cell, parent, incoming, outgoing, includeLoops, recurse)
{
incoming = (incoming != null) ? incoming : true;
outgoing = (outgoing != null) ? outgoing : true;
includeLoops = (includeLoops != null) ? includeLoops : true;
recurse = (recurse != null) ? recurse : false;
var edges = [];
var isCollapsed = this.isCellCollapsed(cell);
var childCount = this.model.getChildCount(cell);
for (var i = 0; i < childCount; i++)
{
var child = this.model.getChildAt(cell, i);
if (isCollapsed || !this.isCellVisible(child))
{
edges = edges.concat(this.model.getEdges(child, incoming, outgoing));
}
}
edges = edges.concat(this.model.getEdges(cell, incoming, outgoing));
var result = [];
for (var i = 0; i < edges.length; i++)
{
var state = this.view.getState(edges[i]);
var source = (state != null) ? state.getVisibleTerminal(true) : this.view.getVisibleTerminal(edges[i], true);
var target = (state != null) ? state.getVisibleTerminal(false) : this.view.getVisibleTerminal(edges[i], false);
if ((includeLoops && source == target) || ((source != target) && ((incoming &&
target == cell && (parent == null || this.isValidAncestor(source, parent, recurse))) ||
(outgoing && source == cell && (parent == null ||
this.isValidAncestor(target, parent, recurse))))))
{
result.push(edges[i]);
}
}
return result;
};
这是堆栈跟踪:
`Uncaught TypeError: Cannot convert undefined or null to object
at NetView.adjustPortPosition (net-view.js:1805)
at mxGraph.mxEventSource.fireEvent (mxEventSource.js:185)
at mxGraph.moveCells (mxGraph.js:6009)
at mxGraphHandler.moveCells (mxGraphHandler.js:1016)
at mxGraphHandler.mouseUp (mxGraphHandler.js:907)
at mxGraph.fireMouseEvent (mxGraph.js:12612)
at mxCellRenderer.<anonymous> (mxCellRenderer.js:851)
at SVGGElement.<anonymous> (mxUtils.js:208)`