Why isn't this query being called?

I'm attempting to use a JS script to call some different queries, everything is working as expected with one exception.

This is the JS script, it calls the get_current_stage query to get the current step in the process. Then stores the column name in a variable to be used in the save_reflection_answer query which is called via event handler when the script successfully completes. All of this works properly.

get_current_stage.trigger
let the_stage = get_current_stage.data
let field_name = "Default"

if (the_stage.reflection_stage[0] === 1){

  increment_stage.trigger
  field_name = "morning_reflection_1"
  
}

if (the_stage.reflection_stage[0] === 2){

  increment_stage.trigger
  field_name = "morning_reflection_2"
  
}

return field_name

The only thing that isn't working is the increment_steps.trigger line which should be called when the appropriate if statement is satisfied. I know the if statements are being satisfied because the field_name vairable is being assigned the proper values, yet the increment_steps query isn't getting called.

This query works fine when manually run, it just isn't getting run from this function and I don't understand why.

Hey @evansignup,

Try the below syntax:

get_current_stage.trigger({
  onSuccess: () => {
    let the_stage = get_current_stage.data;
    let field_name = "Default";

    if (the_stage.reflection_stage[0] === 1) {
      increment_stage.trigger();
      field_name = "morning_reflection_1";
    }

    if (the_stage.reflection_stage[0] === 2) {
      increment_stage.trigger();
      field_name = "morning_reflection_2";
    }

    return field_name;
  }
});

2 Likes

While I much prefer @MiguelOrtiz 's solution I figured I'd go a head and show how to do it using the method you showed incase it's easier to understand:

// .trigger() returns a 'promise', we need to wait for it to finish running before we get the result so we use 'await'
let query_result = await get_current_stage.trigger();
// if the query is a retool db query you'll probably need to change below to query_result.data[0]
let the_stage = query_result.data
let field_name = "Default"

if (the_stage.reflection_stage[0] === 1){

  increment_stage.trigger();  //if increment_stage returns results that we need, call use 'await increment_stage.trigger()' instead
  field_name = "morning_reflection_1"
  
}

if (the_stage.reflection_stage[0] === 2){

  increment_stage.trigger();
  field_name = "morning_reflection_2"
  
}

return field_name
3 Likes

This does work successfully, however when it triggers the SQL query save_reflection_answer that query is now throwing an error:

save_reflection_answer failed (0.38s):column "undefined" of relation "day_log" does not exist

I'm guessing I'll need to tweak the SQL query next?

UPDATE
  day_log
SET
  {{ reflection_question_controller.data }} = '{{ question_input.value }}'
WHERE
  day_log.date = CURRENT_DATE;

This one fails to run, gives an error:

reflection_question_controller failed (0.378s):Cannot read properties of undefined (reading 'reflection_stage')

Yeah, this is likely due to Retool's prepared statements. See useful post on this here.

There's a potential workaround from @pyrrho here

1 Like