Strange behaviour with JS function in workflow

I have a workflow where I am calling a JS function within my JS workflow step.

For testing purposes, all I am doing in my predefined function right now is just returning the input parameter

test_func - param(data)

let in_data = data;

return in_data;

However, when I read this returned data in my workflow step, it is empty.

Workflow code

let output = previousStep.data.data;

output = test_func(output);

console.log(output.created_at[0]);

TypeError: Cannot read properties of undefined (reading '0') (line 10)

Hi @tj11,

All functions in Retool return a promise, so you need to await the result. Try changing your middle line to output = await test_func(output) and see if that works.

Thanks for the suggestion @MikeCB
I tried this too and unfortunately it doesn't seem to be working either

Hey @tj11, console.log(output.data.created_at[0) should work, along with the awaited call to the function, ie: output = await test_func(output) The function call returns an object with a data property where the return value is stored.

Can you share a screenshot of your setup? I just tested this, and it works.

1 Like

Yeah, in the screenshot above you can see that in order to reference 456, you would need result.data

@MikeCB @joeBumbaca

Actually I am trying to read data from a function not the previous step. Here's a screenshot

The dataProcessing workflow step is calling filterSevenDays function on line 18

  1. In order for this to work, you'll need line 22 to be
    const stringDates = output.data.created_at
  2. Can you run the filterSevenDays function with some hard coded test data that is equivalent to the output passed in from dataProcessing?

It looks like that function is returning undefined or running into an error of some sort. Also looks like you are just assigning the input to the new variable in_data and then returning it without making any changes. What are you expecting to happen with ourTimeDate, today and sevenDaysAgo ?

1 Like

It looks like that function is returning undefined or running into an error of some sort. Also looks like you are just assigning the input to the new variable in_data and then returning it without making any changes.

At the moment, this is on purpose just to test function-calling within workflow steps. Initially, I thought filterSevenDays was failing so I just modified it to return the input.

  1. In order for this to work, you'll need line 22 to be
    const stringDates = output.data.created_at

output.data did the trick. So output data from every function is returned inside a data dictionary?

So output data from every function is returned inside a data dictionary?

Yes! The function call returns an object with a data property where the return value is stored.