Diagram.nodes and Diagram.model.nodeDataArray out of sync

In my complex project there is an issue when Diagram.nodes and Diagram.model.nodeDataArray are out of sync after the undo operation.

goJsDiagram.model.nodeDataArray.length === 1
[...goJsDiagram.nodes].length === 4

It is very hard to investigate due to the project’s complexity. Could you give some suggestions what could generally cause that issue?

My guess is that you have some code that is modifying the Model.nodeDataArray without calling a Model method or setting that property.

By the way, goJsDiagram.nodes.count is a dramatically more efficient way to find out how many nodes there are, than building an Array and looking at its length.

All goes through model.addNodeData or model.mergeNodeDataArray.

Well, you could have some unmodeled nodes that your app has constructed, initialized, and explicitly added to the diagram, by calling Diagram.add.

Remember that the Diagram.nodes collection also includes Groups.

So the problem was that the templateMap.add method was accidentally called during undo. After ensuring that the templateMap.add method is called only when intended it looks good.