How can I traverse reverse in figma plugin

In my figma plugin,

I did,
let targetNode = figma.currentPage.findAll((node) => ===;
I can access a node in one of the layer. I am trying to hide everything and from this node, by traversing to root, to set visibility. But I get error to access parent in loop.

Basically trying to do below in a loop

console.log(“current page”,
let node = targetNode[0]

let nodep = node.parent
if(nodep?.type ==“FRAME”)

let nodepp = nodep?.parent
if(nodepp?.type ==“FRAME”)