Iterative Query giving unexpected results

Hello all!

I am running into a few issues while attempting to run the same query with multiple inputs. I would like to input a comma separated list of IDs (in this case, the string "pmd" followed by a sequence of numbers) and a button that triggers a javascript code. My simple example is shown below:

Screenshot 2023-10-05 164730

I then intake the comma separated list and split it by commas, followed by making an array where IDs are grouped into pairs (called pmdSlice). The following code then iterates through each pair, and passes it to the getPMD query, which accesses Benchling and pulls data about the IDs from each slice.

I need to save the data for each pair, so I have a variable called 'holder' that I then push the query data from getPMD to after each trigger of the query.

There seems to be an issue with this triggering/pushing method, as the data that gets stored in the holder variable seems to be the data from just one of the pairs within pmdSlice repeated for however many pairs there are. When looking at the console log, it appears to have run the query successfully for every ID pair passed to it.

Is there a preferred method for going about triggering/storing information for each entry in an array? Is there anything I can be adding to my script to make it not act in this way?

Thank you in advance for the help!

Hey there @nickchav and welcome to the forums!

Check out this example in the docs that covers what you're looking to do.

For a longer explanation, in case it's helpful:

I believe there might be two problems with your code as-is:

  1. query.trigger() is asynchronous; rather than returning a value right away, it returns a Promise that will eventually resolve to the value you're looking for. So immediately after you call getPMD.trigger() the getPMD query you just triggered won't have finished, but your next line of code will still run.
  2. is going to be equal to the value that was returned when the query happened to have last gotten a value back. Since getPMD.trigger() is asynchronous, when you run holder.push(, you're pushing whatever the last-successfully-returned value of the getPMD query was. But the getPMD query you just triggered likely hasn't returned a value yet, so you won't end up pushing the value you expect. All your getPMD queries will still eventually complete (which is what you're seeing in the console), but far too late after you've pushed the original (stale) value into your holder array.

I would try something like:

pmdSlice.forEach(slice => {
  let result = await getPMD.trigger({
    additionalScope: {pmdIDs: slice.join(',')}

let result = await ... will actually wait for the query you just triggered to return before continuing, and assign the returned value of that particular query run to the result variable.

holder.push(result) will then push that actual value into the holder array.

1 Like

Thank you so much, @Taylor_Savage !!! That fixed my problem immediately. I really appreciate it!