How to access JS variable

Hi, I’ve set a JS field that inject the ctx.resource.getFilter() into a JS block inside a popup. but I dom’t know how to access it.

Hi,

In a JS block, any context variable is read asynchronously, and you can first list what is actually visible in that block:

const v = await ctx.getVar('ctx.<path>');  // e.g. ctx.record.id
console.log(await ctx.getVarInfos());       // paths visible in this context

Custom variables are scope-bound: one created in a block’s event flow can only be used inside that block, while one created in the page’s event flow is available to the blocks on that page. A popup is a separate view, so a variable defined outside it may not reach the JS block inside the popup - getVarInfos() shows this immediately.

If what you need is the filter of a specific table, read it where the resource lives instead of passing it through a variable:

const filter = ctx.resource.getFilter();  // in that table's event flow
// or from another block, by UID:
const block = ctx.engine.getModel('<tableBlockUid>', true);
const filter2 = block?.context?.blockModel?.resource?.getFilter();

Docs: ctx.getVar() - NocoBase Documentation and Event flow - NocoBase Documentation

Could you share your NocoBase version, where you created the variable (page or block event flow), and the JS block code? Then I can confirm the exact path for your JS variable.

1 Like

thanks, I figured it out from your answer already