Sharing data between scripts

i have a simple javascript query, call it myscript.js.

var result_array = [];
for(var i = 0; i< tbl_lane_reductions.selectedRow.data.length; ++i){
  var q = tbl_lane_reductions.selectedRow.data[i].myid;
  result_array.push (q);
}

console.log(result_array)

It runs and produces the expected result in console
{0: 'abc', 1: 'def'}

however, when I go to the state /code under myscript, there is no entry for result_array; result_array.data is undefined. What I do see is the full text of my script under the query tab.

I would like to be able to refer to the result in sql script which runs contingently upon the success of this script

how can I do that?

I have some other applications set up similarly where the data from one script can be shared that way, are there any setup issues which could affect this issue?

separately, how would I modify the query above to create an array rather a dictionary. I hope to use the result in SQL as follows

SELECT *
WHERE key in  {{result_array}}

You probably need to return the result at the end of your js query. ie
return result_array;, not just log it to the console. When you run your query in the editor it should show a preview window of the output to confirm it's working as expected.

This might be challenging because Retool uses (correctly imo) prepared statements by default for SQL queries and your IN clause will likely result in an error when it substitutes your array as a string. Might need to rethink how you structure your query or change it to an ANY clause (if your DB supports that) but there's a few threads on here asking that same question if you search for "prepared statements" and "SQL IN clause" or "SQL array"

1 Like

Thanks so much, Dave! When I include the return statement it does indeed show the correct preview. However, it is still not available to the next script, not visible under myscript.result_array.

I don't think it would be identified by that name, only myscript.data - have a look at the state and check it shows a data property with the value you expect and any other script should be able to access that with {{myscript.data}}

2 Likes

thanks so much, it is working now