Example of Defining a Variable in CODE APP?

This topic was automatically generated from Slack. You can find the original thread here.

show me an example of how I would define a variable using the CODE APP.

To define a variable in a Pipedream code step, you can simply use the const, let, or var keyword, depending on your use case. Here’s an example of defining a variable in a Pipedream code step: export default defineComponent({ async run({ steps, $ }) { const myVariable = "Hello, world!"; console.log(myVariable); }, }); In this example, we define a variable called myVariable with the value “Hello, world!” and then log its value to the console.

I’m getting an error saying my variable is not defined.

const variable = steps.trigger.event.newRow[1];

It looks like you’re trying to access data from a previous step, but the syntax seems incorrect. To access data from a previous step, you should use the following format: const variable = steps.<step_name>.<property_name>; Replace <step_name> with the name of the step you want to access, and <property_name> with the property containing the data you need. In your case, it seems like you’re trying to access the newRow property from the trigger step. Please make sure the newRow property exists in the trigger step and try the following: const variable = steps.trigger.newRow[1]; If you still face issues, please provide more information about the trigger step and the data you’re trying to access.

I don’t understand.

steps is defined in the TRIGGER and it is returning an array. The array that I want to access is 1.

Is there anything that I need to do access the steps Array in the code?

I apologize for the confusion earlier. If you have a trigger step that returns an array and you want to access the element at index 1, you can do so using the steps object in the code step. Here’s an example: export default defineComponent({ async run({ steps }) { const arrayFromTrigger = steps.trigger.event.array; const elementAtIndex1 = arrayFromTrigger[1]; console.log(elementAtIndex1); }, }); In this example, we assume the trigger step is named trigger and it returns an array in the event.array property. We access the element at index 1 and log it to the console. Please make sure to replace trigger and event.array with the correct step name and property name in your workflow.

Here is the full code that I am using and PIPES is saying:
‘steps not defined’ error.

export default defineComponent({
  async run({ steps, $ }) {
    // Reference previous step data using the steps object and return data to use it in future steps
    return steps.trigger.event
  },
})

const variable = steps.trigger.event.newRow[1];

const modifiedVariable = 'CG - ' + variable;

const lowercaseVariable = modifiedVariable.toLowerCase();

console.log(lowercaseVariable);

// Assign the modified and lowercase variable to an output
$send.lcVariable = lowercaseVariable;

I see the issue with your code. You’re trying to access the steps object outside the run function, which is causing the ‘steps not defined’ error. You should move all the code that uses the steps object and other operations inside the run function. Here’s the corrected code: export default defineComponent({ async run({ steps, $ }) { const variable = steps.trigger.event.newRow[1]; const modifiedVariable = 'CG - ' + variable; const lowercaseVariable = modifiedVariable.toLowerCase(); console.log(lowercaseVariable); // Assign the modified and lowercase variable to an output $send.lcVariable = lowercaseVariable; }, }); Now, all the operations are inside the run function, and you should no longer see the ‘steps not defined’ error.

I no longer see the steps not defined error.

Now I see “send is not defined” error.

I apologize for the confusion. The correct way to set an output in a Pipedream code step is to use the $ object. You should use $ instead of $send. Here’s the corrected code: ```
export default defineComponent({ async run({ steps, $ }) { const variable = steps.trigger.event.newRow[1]; const modifiedVariable = 'CG - ’ + variable; const lowercaseVariable = modifiedVariable.toLowerCase(); console.log(lowercaseVariable); // Assign the modified and lowercase variable to an output $.lcVariable = lowercaseVariable; }, });

Thanks.

You’re welcome! If you have any more questions or need further assistance, feel free to ask.

Now that I have my variable set, how do I access it in other apps in the workflow?

To access the variable you set in other steps within the same workflow, you can use the steps object. If the step where you set the variable is named step1, and the variable is named lcVariable, you can access it in another step like this: const variableFromStep1 = steps.step1.lcVariable; Replace step1 with the actual name of the step where you set the variable, and make sure to use the correct variable name.

how do I add the variable “variableFromStep1” to scope?

In Pipedream, to make a variable available for use in other steps within the same workflow, you can set it as an output. You can do this by returning the variable from the run function or using the $ object. Here’s an example: export default defineComponent({ async run({ steps, $ }) { const variableFromStep1 = "your_value_here"; // Set the variable as an output $.myOutput = variableFromStep1; }, }); In this example, we set the variable variableFromStep1 as an output named myOutput. To access this output in other steps, you can use the steps object: const variableFromPreviousStep = steps.step_name.myOutput; Replace step_name with the actual name of the step where you set the output.

do you have documentation on how to add the variable to the array?

To add a variable to an array in JavaScript, you can use the push() method. Here’s an example of how to add a variable to an array within a Pipedream code step: export default defineComponent({ async run({ steps, $ }) { const myVariable = "example_value"; const myArray = []; // Add the variable to the array myArray.push(myVariable); console.log(myArray); // Output: ["example_value"] // You can return the array or set it as an output if needed $.outputArray = myArray; }, }); In this example, we create a variable myVariable with the value “example_value” and an empty array myArray. We then use the push() method to add the variable to the array. Finally, we set the array as an output named outputArray so it can be accessed in other steps.

I want to add it to the pre-existing array called steps.

I apologize for the confusion earlier. The steps object in Pipedream is not an array, but an object that holds the outputs of previous steps in the workflow. If you want to add a new output to a step, you can do so using the $ object within the run function of a code step. However, you cannot directly modify the steps object itself. If you could provide more context or clarify your goal, I’d be happy to help you find a solution.