directus
directus copied to clipboard
Flows: Run script doesn't have access to last node's data
Describe the Bug
I have a "Read Data" operation before a "Run Script" operation, however looking at options and payload, the only data available in the data argument sent to "Run Script" is the $trigger, $last and $accountability
. The $last
property seems to be an exact copy of the $trigger.
To Reproduce
Create a flow that fetches some data after the initial trigger, using "Read Data". After that add a "Run Script" operation and just return the data argument as is.
Trigger the flow, and view the logs afterward. Notice the payload on the "Run Script" operation doesn't contain the data form the "Read Data" operation.
Errors Shown
No response
What version of Directus are you using?
9.18.0
What version of Node.js are you using?
16.4.0
What database are you using?
sqlite
What browser are you using?
chromium
How are you deploying Directus?
locally
I am unable to reproduce this with a basic flow unfortunately. This is the flow i've used for testing:
With the following script:
module.exports = async function(data) {
console.log(data);
return {};
}
This logs me all the operator payloads i'd expect.
{ ...shortened... }
The operation is receiving the right data but i think you might be running into this situation (but maybe failing silently 🤔 ) https://github.com/directus/directus/issues/15291
Ok, I forgot about this, one difference is my flow is being triggered by another flow, it is that other flows data that I see in $trigger instead of the primary key which I send as payload
On Tue, 20 Sept 2022, 16:49 Brainslug, @.***> wrote:
The operation is receiving the right data but i think you might be running into this situation (but maybe failing silently 🤔 ) #15291 https://github.com/directus/directus/issues/15291
— Reply to this email directly, view it on GitHub https://github.com/directus/directus/issues/15653#issuecomment-1252469619, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABSVMWTN3S6KY3G7XJVTX6DV7HFF5ANCNFSM6AAAAAAQQOEIQU . You are receiving this because you authored the thread.Message ID: @.***>
@br41nslug not it doesn't seem to be a circular reference issue. I can confirm the $trigger property is that of the "main" flow that was first triggered. I then pass a payload of an array of ids which then call the "child" flow one by one for each id. The value of $trigger is then an ID, which I use in the "Read Data" operation, although the next operation "Run Script" has the data argument it seems of the main flow instead.
@br41nslug I am experiencing the same issue as the OP
. We are using 2 flows, one that ends with Trigger Flow
and one that starts with Another Flow
. What you will find in the data parameter in Run Script
is:
- $trigger: payload of the
Manual
trigger. - $last: payload of the
Read Data
operation. - test_a: same as $last.
You can adjust your reproduction to this:
- Create a new flow that is triggered by
Another Flow
and add theRun Script
there. - Replace the
Run Script
from your data flow, withTrigger Flow
with payload"{{ $last }}"
.
With this setup, I expect the Another Flow
looping the array from Trigger Flow
and Run Script
having a single item from the collection as data.
You can check this by replacing the Run Script
with Log to Console
. You will see that $trigger
or $last
in Log to Console
is not the same as data parameter in Run Script
. And if you add another Run Script
after Run Script
, it won't have the changed payload.
Uhmm.., found out that you can use {{ $trigger }}
and {{ $last }}
in the Run Script
. You can just add it to a variable.. and skip using data parameter in this case.
Same issue here. "Run script" step in the flow-triggered flow receives data from the previous flow trigger. There are no previous steps in the "data"
Trigger flow producing array as an output
Execution flow, executed several times
Read data in the second flow
Run script step detail, returning the input
The payout from the last step, Run script, contains these objects:
- $trigger - contains data from the trigger from the previous flow.
- $last - contains also data from the first trigger.
- accountability
Uhmm.., found out that you can use
{{ $trigger }}
and{{ $last }}
in theRun Script
. You can just add it to a variable.. and skip using data parameter in this case.
Not only {{$trigger}}
etc but also all the other operations keys that are currently in the flows data object can be used via the template variables approach.
E.g. I had a "read users from db into key users
" operation and was currently not able to access it via data.users
. But using the template variable gave me all the values I needed
module.exports = function(data){
const users = {{users}}
...
}
Thanks @abernh . This is the solution!
Same behavior in Condition
operation in a child flow $trigger
value will be from the parent flow