Scripting

Node

11 min
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')

ο»Ώ