elementRegistry.get(id) undefined

image

What do you want to achieve?

Could you share a Codesandbox demonstrating what you are trying to achieve please?

We cannot help you without further info (In your case, without knowing what is bpmnKey variable and what it contains, also what’s in your process. etc.)

setElementsColor in react ,
but 微信图片_20200602154730

elementRegistry.get(id) can’t get the elemnets,
so i can’t use
modeling.setColor(elemnets, {
stroke: ‘red’,
fill: ‘yellow’,
});

We still don’t know what elements exist in your diagram (how can we see if New_activity_risk_audit exists inside your diagram). Please share a sandbox demonstrating what you are trying to achieve, so that we can help you.

i can’t share the whole project , i just want to use

const modeling = Modeler.get(‘modeling’);
const elementRegistry = Modeler.get(‘elementRegistry’);
const elemnets=elementRegistry.get(id)
modeling.setColor(elemnets, {
stroke: ‘red’,
fill: ‘yellow’,
});

to setColor, but it didn’t come true, because elemnets = [],
id is exit , but elementRegistry.get(id) this function seem can’t get the element in react

The element registry does not refer to React or any other js library, it should work the same in every js application. Without sharing your code as a sandbox or giving more context about your diagram, we won’t be able to help you. What does your bpmnKey or bpmn variables look like?

image

importXML takes a callback that will be invoked once the diagram was imported. Only then will you be able to get your element from the element registry.

Simply do:

modeler.importXML(xml, () => {
  const elementRegistry = modeler.get('elementRegistry');

  const element = elementRegistry.get('New_activity_risk_audit ');

  // ...
});
2 Likes

Note that starting from bpmn-js@7.0.0 you may await importXML API too.

See here: https://github.com/bpmn-io/bpmn-js-callbacks-to-promises#after

1 Like

thank you so much :+1:

1 Like