Javascript 为什么在GoJS中diagram.model.nodeDataArray的长度与diagram.FindNodeByExample({})的结果不同

Javascript 为什么在GoJS中diagram.model.nodeDataArray的长度与diagram.FindNodeByExample({})的结果不同,javascript,gojs,Javascript,Gojs,在第一次加载图表时,我在模型中添加了三个元素: var model = new go.GraphLinksModel(); model.nodeKeyProperty = 'goKey'; model.nodeCategoryProperty = 'goType'; model.addNodeDataCollection([ { goKey: 'instance1', goType: 'component', //other data }, {

在第一次加载图表时,我在模型中添加了三个元素:

var model = new go.GraphLinksModel();
model.nodeKeyProperty = 'goKey';
model.nodeCategoryProperty = 'goType';
model.addNodeDataCollection([
  {
      goKey: 'instance1',
      goType: 'component',
      //other data
  }, {
      goKey: 'instance2',
      goType: 'tcp',
      //other data
  }, {
      goKey: 'instance3',
      goType: 'tcp',
      //other data
  }]);
diagram.model = model;
console.log(diagram.findNodesByExample({}).count); //3
console.log(diagram.model.nodeDataArray.length); //3 
然后,我使用diagram.model.removeNodeData方法删除了两个带有goType:'tcp'的项,并将它们再次添加到模型中:

var item2 = _.find(diagram.model.nodeDataArray, {goKey: 'instance2'});
var item3 = _.find(diagram.model.nodeDataArray, {goKey: 'instance3'});
model.removeNodeData(item2);
model.removeNodeData(item3);
console.log(diagram.model.nodeDataArray.length); //1
console.log(diagram.findNodesByExample({}).count); //1

diagram.model.addNodeDataCollection([{
     goKey: 'instance2',
     goType: 'tcp',
     //other data
  }, {
     goKey: 'instance3',
     goType: 'tcp',
     //other data
}]);
但在此之后,图中的节点数量有所不同,我在画布上只看到两个节点:

console.log(diagram.model.nodeDataArray.length); //3
console.log(diagram.findNodesByExample({}).count); //2
如果使用each方法查看diagram.findNodesByExample({})的结果,我发现只添加了实例2

diagram.findNodesByExample({}).each(function (item) {
   console.log(item.data.goKey);
});
// instance1
// instance2

我做错了什么?

我刚刚尝试了您的代码,但无法重现问题。这是我使用的整个页面:

<!DOCTYPE html>
<html>
<head>
<title>Minimal GoJS Sample</title>
<!-- Copyright 1998-2017 by Northwoods Software Corporation. -->
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/1.7.28/go-debug.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<script id="code">
  function init() {
    var $ = go.GraphObject.make;

    diagram =
      $(go.Diagram, "myDiagramDiv",
        {
          initialContentAlignment: go.Spot.Center,
          layout: $(go.GridLayout)
        });

    diagram.nodeTemplate =
      $(go.Node, "Vertical",
        $(go.TextBlock, new go.Binding("text", "goKey")),
        $(go.TextBlock, new go.Binding("text", "goType"))
      );

    var model = new go.GraphLinksModel();
    model.nodeKeyProperty = 'goKey';
    model.nodeCategoryProperty = 'goType';
    model.addNodeDataCollection([
      {
        goKey: 'instance1',
        goType: 'component',
        //other data
      }, {
        goKey: 'instance2',
        goType: 'tcp',
        //other data
      }, {
        goKey: 'instance3',
        goType: 'tcp',
        //other data
      }]);
    diagram.model = model;
    console.log(diagram.findNodesByExample({}).count); //3
    console.log(diagram.model.nodeDataArray.length); //3 
  }

  function replaceTwo() {
    var model = diagram.model;
    model.startTransaction();
    var item2 = _.find(model.nodeDataArray, { goKey: 'instance2' });
    var item3 = _.find(model.nodeDataArray, { goKey: 'instance3' });
    model.removeNodeData(item2);
    model.removeNodeData(item3);
    console.log(model.nodeDataArray.length); //1
    console.log(diagram.findNodesByExample({}).count); //1

    model.addNodeDataCollection([{
      goKey: 'instance2',
      goType: 'tcp',
      //other data
    }, {
      goKey: 'instance3',
      goType: 'tcp',
      //other data
      }]);
    model.commitTransaction("replace two");

    console.log(diagram.model.nodeDataArray.length); //3
    console.log(diagram.findNodesByExample({}).count); //2??? -- No, I get 3
    diagram.findNodesByExample({}).each(function(item) {
      console.log(item.data.goKey);
    });
  }
</script>
</head>
<body onload="init()">
  <div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
  <button onclick="replaceTwo()">Replace Two</button>
</body>
</html>

最小GoJS样本
函数init(){
var$=go.GraphObject.make;
图解=
$(go.Diagram,“myDiagramDiv”,
{
initialContentAlignment:go.Spot.Center,
布局:$(go.GridLayout)
});
diagram.nodeTemplate=
$(go.Node,“垂直”,
$(go.TextBlock,新的go.Binding(“text”,“goKey”),
$(go.TextBlock,新的go.Binding(“text”,“goType”))
);
var model=new go.graphlinkmodel();
model.nodeKeyProperty='goKey';
model.nodeCategoryProperty='goType';
model.addNodeDataCollection([
{
goKey:'实例1',
goType:'组件',
//其他数据
}, {
goKey:'实例2',
goType:'tcp',
//其他数据
}, {
goKey:'实例3',
goType:'tcp',
//其他数据
}]);
diagram.model=模型;
console.log(diagram.findNodesByExample({}).count);//3
console.log(diagram.model.nodeDataArray.length);//3
}
函数replaceTwo(){
var模型=图表模型;
model.startTransaction();
var item2=u2;.find(model.nodeDataArray,{goKey:'instance2'});
var item3=u2;.find(model.nodeDataArray,{goKey:'instance3'});
模型。removeNodeData(第2项);
模型。removeNodeData(第3项);
console.log(model.nodeDataArray.length);//1
console.log(diagram.findNodesByExample({}).count);//1
model.addNodeDataCollection([{
goKey:'实例2',
goType:'tcp',
//其他数据
}, {
goKey:'实例3',
goType:'tcp',
//其他数据
}]);
示范交付交易(“替换两个”);
console.log(diagram.model.nodeDataArray.length);//3
log(diagram.findNodesByExample({}).count);//2???--不,我得到3
findNodesByExample({}).each(函数(项){
console.log(item.data.goKey);
});
}
替换两个

问题终于找到了。从模型中删除节点(我将它们保存在副本中)后,我再次添加了它们,因此,如果查看这些节点,我们将看到一个额外的属性\uuuu gohashid,在再次将其添加到模型之前,应该删除该属性。我不知道它是如何工作的,但是这串代码

delete node.__gohashid;

修复了上述问题。希望它对某些人有用。

谢谢您的回答,但问题在于重新使用删除的节点。删除_gohashid后,它已被修复。