Unable to run for loop within starter script
Hi,
I am using a starter script to loop over the elements of the results of a Supabase get row output.
I have set the output for the Supabase get row to be an array but when I try an loop over the elements I dont see the for loop executed.
Code in the starter script.
"""
logging.log("Inside starter script"); // Logs(Inside starter script)
logging.log("Input : "+typeof(input_array)); // Logs(Input : object)
logging.log("Input length: "+input_array.length); // Logs(Input length: 2)
for(let i=0; i< input_array.length; i++)
{
logging.log("Inside for loop"); // Not logged
logging.log("Data1:" + input_array[i]['columnA']); // Not logged
logging.log("Data2 :" + input_array[i]['columnB']); // Not logged
}
"""
When I run my workflow the first 3 logging requests are printed but code within the loop is not executed
Solution:Jump to solution
🤖 AI Response
Your issue likely stems from the fact that the Supabase response is returning a JSON object that resembles an array (with a length property), but is not a true JavaScript array. This can happen if your database column is of type JSON/JSONB and the data structure is not strictly an array, or if the API response is being parsed in a way that results in an object rather than an array.
To handle this, you should:
- Check the actual data type returned by Supabase. If it's not a standard array, you can convert it using
Object.values(input_array)
to get an array of the object's values, or use Object.keys(input_array)
to iterate over its properties, as you suggested.
- Ensure your Supabase query or function returns an array if that's what you expect. If you're using a Postgres function, make sure it returns a set or array, not a JSON object with numeric keys.
- If you're storing arrays in a JSON/JSONB column, remember that Postgres supports both native arrays and JSON arrays. Native arrays are returned as arrays, while JSON/JSONB columns can store any JSON structure, including objects or arrays.
If you want to guarantee array behavior in your JavaScript code, your approach of checking with Array.isArray()
and converting as needed is correct. Also, review your Supabase node or API configuration to ensure it returns the expected data structure.
🔗 References:
1. https://github.com/orgs/supabase/discussions/6008
2. https://community.flutterflow.io/ask-the-community/post/convert-dynamic-json-data-to-supabase-row-data-type-lfW25sPWfeH7TxT
3. https://supabase.com/docs/guides/database/json
4. https://www.youtube.com/watch?v=nxeUiRz4G-M
5. https://supabase.com/docs/guides/database/arraysBuildShip AI Support
2 Replies
Processing...
🤖 Sifting through digital archives. 🗄️ this might take a couple of minutes... beep boop
Solution
🤖 AI Response
Your issue likely stems from the fact that the Supabase response is returning a JSON object that resembles an array (with a length property), but is not a true JavaScript array. This can happen if your database column is of type JSON/JSONB and the data structure is not strictly an array, or if the API response is being parsed in a way that results in an object rather than an array.
To handle this, you should:
- Check the actual data type returned by Supabase. If it's not a standard array, you can convert it using
Object.values(input_array)
to get an array of the object's values, or use Object.keys(input_array)
to iterate over its properties, as you suggested.
- Ensure your Supabase query or function returns an array if that's what you expect. If you're using a Postgres function, make sure it returns a set or array, not a JSON object with numeric keys.
- If you're storing arrays in a JSON/JSONB column, remember that Postgres supports both native arrays and JSON arrays. Native arrays are returned as arrays, while JSON/JSONB columns can store any JSON structure, including objects or arrays.
If you want to guarantee array behavior in your JavaScript code, your approach of checking with Array.isArray()
and converting as needed is correct. Also, review your Supabase node or API configuration to ensure it returns the expected data structure.
🔗 References:
1. https://github.com/orgs/supabase/discussions/6008
2. https://community.flutterflow.io/ask-the-community/post/convert-dynamic-json-data-to-supabase-row-data-type-lfW25sPWfeH7TxT
3. https://supabase.com/docs/guides/database/json
4. https://www.youtube.com/watch?v=nxeUiRz4G-M
5. https://supabase.com/docs/guides/database/arraysBuildShip AI Support