Scripting

Node

11min
Document image
๏ปฟ

The Node action is one of the actions at your disposal that allows you to execute Javascript code.

Module imports can be performed using CommonJS require('module') syntax only. The list of currently allowed modules can be found below:

Use the return keyword to set the output value. You can return either Single Values or List Values, not both at same time.

Timeout: โ†’ Integer representing execution time limit(when timeout limit is reached, the action will throw an error) Single result: โ†’ Output that will be set for single value outputs. List result: โ†’ Output that will be set for list value outputs. Error: โ†’ String representing errors resulted from the execution of the script (the output is set only when an error occurs, otherwise itโ€™s null)

Something to consider before trying to use the Node action to automate your use cases is that it does not support working with files.

How to configure the Node action?

  1. Create a process and name it.
  2. Drag the Node action to the canvas and link it to the other actions.
Document image
๏ปฟ

3. Create the variables needed for the configuration of the action, and then add them to the configuration panel.

4. Add your Javascript Code to the editor.

5. Configure the output variables which will store the results.

Document image
๏ปฟ

6. Save, Validate and run.

7. Click Checked Instance to view the results. You will see that the outcome of the interpreted code is saved in the result variable. The script ran without errors so the value of the errorMessage variable is null.

Document image
๏ปฟ

Usage Examples:

1. Timeout:

returnย (asyncย ()ย =>ย { ย ย ย ย awaitย newย Promise(resolveย =>ย setTimeout(resolve,ย 10000)) //This code waits for 10 seconds. })()

Obs: If you set the Timeout to 5 seconds, the action will throw an error at runtime.

2. Scripts with Single outputs.

๏ปฟInteger๏ปฟ

var a = 1 var b = 2 return a + b

๏ปฟFloat๏ปฟ

varย aย =ย 1.0 varย bย =ย 2.5 returnย aย +ย b

๏ปฟString๏ปฟ

varย aย =ย "aa" varย bย =ย "bb" returnย aย +ย b

๏ปฟBoolean๏ปฟ

varย aย =ย 1 varย bย =ย 2 returnย aย ==ย b

๏ปฟJson / DataModel๏ปฟ

var x = { "user": { "id": 1, "name": "Someone", "height": 170.5 } } return x

๏ปฟDateTime๏ปฟ

letย dย =ย newย Date() returnย d

Obs: These should only set the Single output. Should work for all types.

3. Scripts with List outputs.

letย dย =ย newย Date(); letย daย =ย newย Date(); returnย [d,ย da]

Obs: These should only set the List output. Should work for all types, just like single values. 4. Basic module usage

If the module does not support CommonJS require, we canโ€™t use it

var _ = require('underscore'); return _.map([1, 2, 3], function(num){ return num * 3; })
const R = require('ramda'); const greet = R.replace('{name}', R.__, 'Hello, {name}!'); return greet('Alice');
let cheerio = require('cheerio') let $ = cheerio.load('<h2 class="title">Hello world</h2>') $('h2.title').text('Hello there!') $('h2').addClass('welcome') return $.html()

5. Syntax errors

varย xย =ย 1 returย x

Obs: Syntax errors will only set the Error field. 6. Throw behavior

varย xย =ย 1 throwย "Notย onย myย watch" returnย x

Obs: Exception messages thrown using throw โ€œmessageโ€œ syntax will show up on the Error field. Only Error will be set after execution. 7. Async / Await

const axios = require('axios') const findInDictionary = async (word) => { const response = await axios.get(`https://api.dictionaryapi.dev/api/v2/entries/en/` + word) return response.data }; return findInDictionary('dog')

๏ปฟ

Updated 16 Feb 2024
Doc contributor
Doc contributor
Did this page help you?