Invalidate and remove the link if the link is connecting to default port of a node

I have two nodes

  1. In node1 I have a port
  2. In node2 I don’t have a port

My code is creating an invalid link from node1 to node2. In link data toPortid we are getting like toPortId=‘i’. Even though I don’t have any ports in node2, The link is getting connected to default port of a node.

I need to invalidate/remove this kind of invalid links. Please help.

So you are not talking about the user’s interactive drawing of a new link or reconnecting of an existing link? You are only talking about your code creating an invalid link? Well, if it hurts, don’t do it – make sure your code operates correctly according to your requirements.

If instead you are talking about checking model data that some independent process is creating and passing to you (i.e. not your code), then I suppose what I would do is have a pass that examines all of the links right after loading a model. Something like:

// load the model
myDiagram.model = ...
// check for any invalid Links
const invalids = new go.Set();
myDiagram.links.each(link => {
    if (... link is not valid ...) invalids.add(link);
// now either highlight the bad links and tell the user, or just delete them
myDiagram.commit(diag => diag.removeParts(invalids));

Thanks Walter for the response. I decided to delete that invalid links as you are suggesting. One thing I didn’t understand is if (… link is not valid …). How can I find the invalid link(The link which is connecting to default port of a node).

That depends on how you defined your node template. Maybe:

  if (link.fromPort === link.fromNode || link.toPort === link.toNode) ...

Thanks Walter. I have implemented as you told. It’s working fine.
Thank you so much.