Looping a JS code

Hi! I experimented and looked for answers but nothing worked. Hope someone will be able to help me out :crossed_fingers:

I need to run a bunch of lopped nodes with an array as an input. I managed to make it work for all necessary API requests and Google Sheet update. But I
still have to do a reverse geolocation based on lat/long data retrieved from an API call (qrVehicleStateLoop) (returned json on screenshot)

I have a python code that works really well for me but I need to rewrite it to work in JS as I cannot use Python in loops.

Flow structure (screenshot):

Python code:

from geopy.geocoders import Nominatim

def get_address(latitude, longitude):
    # Ensure that the user_agent is specific to your application or use case
    geolocator = Nominatim(user_agent="Dott")
    location = geolocator.reverse((latitude, longitude), exactly_one=True)
    return location.address if location else "Address not found"

# Example usage:
latitude = qrVehicleBrusselsCity.data[0].gnss.point._latitude  # Example latitude (for New York City)
longitude = qrVehicleBrusselsCity.data[0].gnss.point._longitude  # Example longitude
address = get_address(latitude, longitude)
return(f"The address for the coordinates is: {address}")

Does anyone have any tips?

One thing you can try is to create a separate workflow with your python code that accepts the qrVehicleStateLoop lat/long output data as its input.

Then, you should be able to call upon that workflow as kind of a python subroutine for your loop in the main workflow to get the full set of data you need for the last googleSheetsAppend loop.

Caveat Emptor: this would be considered another workflow run, I think, so if you have a limited number of runs per cycle this might not be the most ideal solution.

Hi @adibandzioch,

Another way to run python in loops would be to create a function that runs python code and call it from the javascript loop lambda:

Let me know if that works for your use case!

2 Likes

Thank you! I only had time to look at it now, this makes sense, cool idea.

After unsuccessfully trying a bunch of options - in my function, how do I refer to the values generated in the API call I made in the loop?

qrVehicleStateLoop runs an API request for each of values (this works well) but how do I refer to its results in the function?

In my python code the necessary value would be:

longitude = qrVehicleBrusselsCity.data[0].gnss.point._longitude


If you need to access a value from a previous block in a function, you will have to pass it as a parameter. For example:

You can also pass value and index that are available for each iteration of the loop:

Does that address your use case?

Thank you.

When I refer to it as value, it returns a whole array but I only need to refer to one of the values from the array.

I'd like this to work :dotted_line_face: --> value.data[0][0].gnss.point._latitude

My value is nested under the array returned from the loop.

Sorry, not sure if I'm clear enough! Some screenshots:

Hello!

Can you try completely removing your code lines 9-13 and replacing the latitude/longitude parameters with param1 and param2?

It would look more like:
lines 1-8 remain unchanged but line 16 becomes

address = get_address(param1, param2)

Hmm it doesn't work as it would be an incorrectly passed parameter.

When I try to return the value from my loop in a JS as "qrVehicleStateLoop.data[0][0].gnss.point._latitude" I'm getting the same value 3 times.

@andrei any ideas?

I'll need a value from "qrVehicleStateLoop.data[0][0]", "qrVehicleStateLoop.data[1][0]", "qrVehicleStateLoop.data[2][0]" and so on for as entries as returned in the loop.

You should be able to use index as the loop iteration value in your code:

return qrVehicleStateLoop.data[index][0].gnss.point._latitude

1 Like

Thx Pyrrho! This is one step forward for me but not yet there.

What I have now:

-> qrVehicleStateLoop with 2 values I need to use in my python code:

qrVehicleStateLoop.data[index][0].gnss.point._latitude,
qrVehicleStateLoop.data[index][0].gnss.point._longitude

-> function1 with 2 parameters and code as below:

from geopy.geocoders import Nominatim

def get_address(latitude, longitude):
    # Ensure that the user_agent is specific to your application or use case
    geolocator = Nominatim(user_agent="Dott")
    location = geolocator.reverse((latitude, longitude), exactly_one=True)
    return location.address if location else "Address not found"
  
address = get_address(param1, param2)
return(address)


-> JS with input from qrVehicleStateLoop: "return (await function1.data)"

There's definitely something wrong with JS but I don't know what. Any help appreciated.




@andrei Hi! I tried to follow your logic but there's a lot of missing from my knowledge. Not sure if I managed to do anything right? I'd love to get some more input :crossed_fingers:

Hey there!

I think this is a bit wonky in that you are saying "return location address" but then you are trying to do it based on conditions after the fact. I am pretty sure that this will just attempt to return location.address and ignore the if/else you wrote afterwards. I'd have written this more like:

if (location) return location.address 
else return "Address not found"

Hi, thx for your help! I managed to make it work by making some changes to my python and JS code. Attached in case anyone is curious.


1 Like