Can't get color in some nodes

Pretty new with this.

I want to make a plugin to select the right nodes (vectors within an illustration, which is an instance) with certain color and this code works with one component instance but not another. It happens on about 20% of the illustrations I have although they have no discernable difference.

let rightElements = myIllustration[0].findAll(n => n.type === 'VECTOR' && n.fills[0].color.r === 1)
figma.currentPage.selection = rightElements

The error is Error: in findAll: "findAll" callback crashed: TypeError: cannot read property 'color' of undefined

I can’t see any discernable difference between the nodes in each instance. They are all vectors, the exact same color. I am able to get the color when I click on the vector I wanted to select and do a console log.

Some node does not have fills, so it is impossible to access the properties along this path.

Thanks! How can I use findAll only for nodes with fills?

Write a condition where you check each property to make sure it exists.