Using a JS Query result as an argument for a PostgreSQL query?

Hey all :wave:
So, I have a multiselect component and it has a bunch of values in it like ["a","b","c"]. When these values are unselected, I want to set values of the columns in the DB to 0. My JS code is:

<script>
var tagsTot = ["a", "b", "c"];
document.write("SET ")
for (var i=0; i<=tagsTot.length-1; i=i+1) {
  document.write(tagsTot[i]);
  if (i <= tagsTot.length-2){
  document.write(" = '0', ");
  }
  else{
  document.write(" = '0' ");
  }
}
</script>

Which has the result of:

SET a = '0', b = '0', c = '0'

I want to use this result in my PostgreSQL query. Many thanks in advance.

Hey @kevinTDS! Using the current structure you have, just update this to use a normal function return instead of document.write. So something like:

var tagsTot = ["a", "b", "c"];

let s = "SET "

for (var i=0; i<=tagsTot.length-1; i=i+1) {
  s += tagsTot[i];
  if (i <= tagsTot.length-2){
    s += " = '0', ";
  }
  else{
    s += " = '0' ";
  }
}

return s

Then you should be able to reference the returned result as query.data. LMK if this works!

1 Like

Thanks @justin, you're a legend!

1 Like