1

I’m working on a Directus flow where I’m using a Run Script operation to add a modifiedPayload object to the data object. The modifiedPayload is successfully created and logged in the Run Script, but when I try to access it in subsequent operations (e.g., Log or Webhook), it returns undefined. Here’s the Run Script code I’m using:

`module.exports = async function (data) {
  // Create a timestamp
  const timestamp = new Date().toISOString();

  // Extract date (YYYY-MM-DD) from the timestamp
  const date = timestamp.split('T')[0];

  // Extract time (HH:MM:SS) from the timestamp
  const time = timestamp.split('T')[1].split('.')[0];

  // Create a new object with the additional fields
  const modifiedPayload = {
    ...data.$trigger.payload, // Spread the original payload
    timestamp,
    date,
    time,
    location: "Office A" // Replace with dynamic logic if needed
  };

  // Log the modified payload for debugging
  console.log("Modified Payload in Run Script:", modifiedPayload);

  // Return the modified data
  return {
    ...data, // Spread the original data
    modifiedPayload // Add the modified payload as a new property
  };
};`

After the Run Script, I’ve added a Log operation to verify the modifiedPayload:

Message: Modified Payload: {{ modifiedPayload }}

However, the log shows undefined for modifiedPayload. Payload After Run Script:

`{
  "$trigger": {
    "event": "attendances.items.create",
    "payload": {
      "event_type": "check-in",
      "user_id": "51a12d76-b674-4018-96b6-da95dc5115e2"
    },
    "key": 90,
    "collection": "attendances"
  },
  "$last": null,
  "$accountability": {
    "role": "a846c34e-cb97-48e6-8e4f-87f92a570bfd",
    "user": "9e4032ca-4676-4c89-937e-77f3c80e3c2a",
    "roles": [
      "a846c34e-cb97-48e6-8e4f-87f92a570bfd"
    ],
    "admin": true,
    "app": true,
    "ip": "127.0.0.1",
    "userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36",
    "origin": "http://0.0.0.0:8055",
    "session": "81oeUm9_Sd5KKfmGazQZqFHPWHZaQ5d6FSW4HhWt39KhQ8XQx2HeIrqM7aai1vwg"
  },
  "$env": {},
  "event_type": null,
  "modifiedPayload": {
    "event_type": "check-in",
    "user_id": "51a12d76-b674-4018-96b6-da95dc5115e2",
    "timestamp": "2025-03-22T18:17:24.090Z",
    "date": "2025-03-22",
    "time": "18:17:24"
  }
}`

What I’ve Tried:

Verified that the Run Script is correctly adding modifiedPayload to the data object.

Added a Log operation after the Run Script to verify modifiedPayload.

Ensured the flow is configured to trigger on the correct event (items.create for the attendances collection).

Checked permissions for the user role to ensure access to the flow and collections.

Expected Behavior:

The modifiedPayload should be accessible in subsequent operations (e.g., Log or Webhook).

Actual Behavior:

The modifiedPayload is undefined in subsequent operations.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.