Run multiple queries in JS query and only return if query does not equal 0

  • Goal: Have 3 queries run in JS. if query 1 response == 0, then run query 2. if query 2 == 0 then run query 3. If query1 response !=0, then return that response. If query2 response != 0, then return query 2 response. All has to be done in one block as the response of that query will be showcased in table

current JS:
const find1 = await query1.trigger()

if (find1 !== 0) {
return find1
} else {
const find2 = await query2.trigger()
if (find2 !== 0 ){
return find2
} else {
const find3 = await query3.trigger()
return find3
}
}

Currently this code only returns query3 if != 0, but not for query 1 or 2 if != 0. Any thoughts?

Thank you for creating this topic so we can follow up after Office Hours.

Your idea of running the same query based on the value of State variables and update those depending on the output of query1 and query2 should work! To run the same query with a different scope, we could use a combination of a "Run script" event handler and Additional Scope.

Questions:
in your code is the phrase 'refrence_to_state' is the new value for the variable?

Also how do you trigger to run this query again?

Lastly, say the query 1 == 0 then I only want to change 1 variable, re run query if it still == 0, then we will change the other variable and run it again

Basically the query is a search query and has 3 levels. if the search comes empty, then remove level 3 with empty string, and re-run with levels 1 and 2. if still 0, then make level 2 variable empty string, and re-run query

"level1": {{Level_1_variable.value}},
"level2": {{level2variable.value}},
"level3": {{level3variable.value}}

Figured it out. What I did is used query 1,2,3. If query 1 returns a response, set variableA to that response. if query 1 response == 0, run query 2. If query 2 has a response, set variableA to query 2's response, etc. all done in event handlers.

1 event handler to trigger other query based on response.
another event handler to set variable value based on query response

1 Like

Amazing! :sunglasses:

Hello Paulo, I ran into a new issue with this solution. Currently I have query 1,2, and 3 synced up to my solution shown above. However, In order to run an Update query based on the response of query 1,2, or 3 I have to reference the id from the response. I currently do this by having the key: value pair look like this:

"id": {{query1.data['0']._id}}

In order to have 1 update query, I need to reference the id from the query that had the response. Since I can do something like this:

"id": {{query1.data['0']._id || query2.data['0']._id || query3.data['0']._id}}

Need JS like in my initial ask to run these 3 queries in the same block of code based on in the response == 0

This will set the id key to the first "queryName.data['0']._id" that has a truthy value (not 0 in our case), which is a great idea! Could you share a screenshot of where this is set?

On the other hand,

I'm not sure if I understand the current issue. From what you shared above, it looks like we are taking the 3 queries approach (instead of 1). The "or" statement should set the id to the correct value because it will only be the output of query2 if query1 evaluated to 0, and it will only be the output of query3 if the other two queries evaluated to 0 (0 || 0 || 3 evaluates to 3).

If now we just need the data from the first query that did not evaluate to 0, in other words, the query that set the value for the id key. We could just hold to that id in a state variable and use an if statement to see which query had that output.

if (stateVariableId === query1.data['0']._id ){
  return query1.data
} else if (stateVariableId === query2.data['0']._id){
  return query2.data
} else {
  return query3.data
}

Unless the three queries on the or statement are different queries than the ones we are evaluating for the output of the JS query.

tiny suggestion here to cover an edge case when all of the queries return 0:
"id": {{ query1.data['0']._id || query2.data['0']._id || query3.data['0']._id || (function(){throw "all queries returned 0"}()) }}

you could put a default value for 'id' instead of trying to throw an error by replacing (function(){throw "all queries returned 0"}()) with 'my_default_id_value', but that could make problems later on in the block

also, just wanna double check and make sure .data['0'] was on purpose cause it's actually different than .data[0]. this might not be what you intended as i believe it would actually evaluate to a key/value pair inside the .data json object that is named '0' instead of accessing an array index:

'query1': {
  'data': {
    '0': "my key/value pair named the number '0' as a string"
  }
}

vs

'query1': {
  'data': ['my value at index 0']
}

I'm not sure I completely understand all of the conditions for running or re-running each of those 3 queries. do you happen to have any code you could share with us for this single js block that triggers 3 queries? or a flow chart? that'd work too. it doesn't matter if the codes broken really, just something so we can get a better understanding of what you're looking for. don't forget to censor any sensitive info (i just use mspaint and make ugly squiggly lines over stuff :nerd_face: )

1 Like