Collection was modified during iteration: Set(Part)#453

Hi ,
I’m trying to prevent the drop of some objects relaying on their category , i’ve done this :


myDiagram.addDiagramListener(“ExternalObjectsDropped”, (e) => {
if (myVar != null) {

e.subject.each(function (node) {
    if ( === "sink" || === "separator") {


but it didnt work for me , it shows this error :


please help me !

The error says that you cannot modify a collection while you are iterating over it. The error is meant to avoid bugs where as you remove items from the collection what you iterate over might not be well defined.

Your iteration e.subject.each(... myDiagram.commandHandler.deleteSelection ...) is trying to delete all selected Parts at each step. Clearly that’s not what you want to do.

More plausible is that within each iteration you want to remove those individual parts that meet your criteria, but you want to leave the rest there: e.subject.each(... e.diagram.remove(node) ...). But because removing a node deselects it, this means you are change the Diagram.selection collection, so you’ll get that error again.

The solution is to do something like:

e.subject.copy().each(function(node) {
    if (...) node.diagram.remove(node);

I hope that works for you.

yes thank you so much , it worked :D