Diagram.nodes contains phantom nodes when adding a link

I have a ModelChangedListener to execute some logic when a link is added:
(e.change === go.ChangedEvent.Insert && e.propertyName === "linkDataArray")

Within this listener, the diagram.nodes array contains 4 elements, even though my model and nodeDataArray only contains two nodes. The diagram.parts also contains only 2 elements.

I tried to find out what the two additional nodes are, but they have no name, no size and no data.

Do you know what these two phantom-nodes could be and has this changed since goJS 1? I get an error now because I am accessing node.data which is undefined for the phantom nodes. The same code has worked before but I am not sure if this is related to the goJS upgrade or some side effect of my other code.

No, nothing like that has ever changed.

So you have six separate non-Link Parts, even though your Model.nodeDataArray only has two objects in it? Presumably two of your Nodes actually correspond to those two data objects, yes?

Do you have any code that calls Diagram.add? That might explain the additional Nodes or simple Parts. For example, if you have some simple static background Parts that do not belong to the Model, they would be added via code, and would explain the Diagram.parts collection.

Also, during the operation of some Tools, there may be temporary parts created and used. For example, the LinkingTool has two temporary Nodes and one temporary Link that is what the user sees and manipulates during the operation of that tool. Those two Nodes and one Link are added to the Diagram when the tool starts and are removed when the tool stops.

What is temp1 in your console?

I have

model.nodeDataArray: 2
diagram.parts: 2
diagram.nodes: 4

Two of the nodes correspond to the parts/entries in nodeDataArray.

I don’t have calls to Diagram.add, but it seems the two additional nodes only exist when creating a new link. So I assume that during the ModelChangedEvent for new links (e.change === go.ChangedEvent.Insert && e.propertyName === "linkDataArray") the two temporary nodes still exist?

If this is the case, I need to make sure to check that the node.data is actually set when iterating over the diagram.nodes. Until now I was assuming that the diagram.nodes would only contain my own nodes. Or is there some other way to get only my “own” nodes?

(I don’t know what “temp1” is, accidentially created it while debugging)

OK, I didn’t know that the new link was being added by the LinkingTool. That would explain the additional two Nodes.

That is correct – the two temporary nodes and one temporary link used by the LinkingTool are in the Diagram but not in the Model. They are “unmodeled” Parts. (You might want to check diagram.links.count to confirm.)

When are you looking at this state – which event(s) or method override(s)?

I am listening to the ModelChangedEvent and executing the functionality when these conditions apply:
e.change === go.ChangedEvent.Insert && e.propertyName === "linkDataArray"

I don’t think I had the two temporary nodes here with goJS 1, but I am not sure if I changed anything else in my code which might cause this behaviour.
Anyways, I now only use the nodes for which node.data exists instead of using all nodes in the collection.

Usually model ChangedEvent listeners only care about the Model or its data. After all, there might be more than one Diagram showing that model.

Those unmodeled parts normally have Part.data null.