How to get values in Loop from a block that is not being iterated on

So I have a 3 block workflow I am trying to create.

  1. In block 1, doing a mySQL query to get transactions created in last 30 mins.
  2. In block 2, doing a loop on block 1 and hitting google API to get distance between two points from my block 1 data for each row.
  3. In block 3, another loop and I want to save this data I got from block 2 using a backend API.

The problem I am having is that, to hit the backend API I need data from both block 1 and block 2. As block 1 contains the ID of the transaction which I need to update and block 2 contains the data. But on loop when I use {{value}} it can only get data for the one I am iterating on.

Can you please help on how I can get this done?

Hi @amany112

I've solved a similar problem using code instead the GUI in the loop Block, such as:

return await Promise.all(
  myQuery.data.map(async row => {
    const res = await queryLoop_lambda.trigger({
      additionalScope: {
        value: row
      }
    })
    row.myNewProp = res.data.someProp // that's the magic
    return row
  })
)

Hope this help.

2 Likes

Thanks @abusedmedia.

I took me a while to get this right :sweat_smile:. Another thread helped me get to the solution, thanks to the detailed explaination by the OP How to do an action based on the results of each REST call in a loop - #2 by Jonnalo

So basically, the solution of adding code instead of the GUI has to be done on the Code block where you are using a resource query to fetch some data (in my case, Block 2). Simply get the data being fetched from the resource and append it to the row which has the data coming from previous query so that everything in a single data structure.

My updated code for block 2, pretty much exactly as suggested by @abusedmedia

return await Promise.all(
  getTransactions.data.map(async row => {
    const res = await googleAPI2_lambda.trigger({
      additionalScope: {
        value: row
      }
    });
    row.googleResponse = res.rows
    row.distance = res.rows[0].elements[0].distance.value
    row.duration = res.rows[0].elements[0].duration.value
    return row
  })
)

Hope this helps any future strugglers like myself!

1 Like