Skip to main content

The function node

Use the function node to write custom JavaScript code for complex calculations, data transformations, and message processing logic. It lets you implement logic that standard nodes don't support.


function node settings​

"Node Information" window of the function node "auth data": the "Function" field contains code that sets grant_type to client_credentials and the Content-Type and Accept headers

1. Name​

The node name.
The name appears in the workspace and helps you identify the node.
If you leave it empty, the node is named function.

Example:

  • Node name: auth data

2. Function​

The JavaScript code that runs for each message passing through the node.

Input data format:​

The code receives a msg object that represents the current message.

Return data format:​

  • To pass data down the flow, the node must return a message object (or an array of objects).
  • If you return null, the message isn't passed on.

Example:

// Increment msg.payload by 1
msg.payload = msg.payload + 1;
return msg;

3. Output Example (JSON)​

An example of the data the function returns, in JSON format.
The data schema for the next node is built from this example: its fields appear in the Input Schema block of the next node. The editor checks the example against the function code.

The example's fields describe the root of the msg object, not msg.payload, because the function returns the entire message.

The example also serves as test data: if you click Execute in one of the next nodes (admin-api, http request) and the function doesn't pass data on, the value of this field is used.

Example:

{
"payload": {
"orderId": 123,
"status": "paid"
}
}

Usage examples​

Example 1: Multiplying the value in msg.payload​

msg.payload = msg.payload * 2;
return msg;

Description:

  • Multiplies msg.payload by 2 and sends the updated object.

Example 2: Adding a new property​

msg.newProperty = "Hello, World!";
return msg;

Description:

  • Creates a newProperty property and adds it to the msg object.

Example 3: Conditional message processing​

if (msg.payload > 100) {
msg.alert = "High value detected!";
} else {
msg.alert = "Value is normal.";
}
return msg;

Description:

  • Adds an alert property with a text warning that depends on the value of msg.payload.

Example 4: Filtering messages​

if (msg.payload > 50) {
return msg; // Pass the message on
}
return null; // Stop the message

Description:

  • Passes on only messages with msg.payload > 50 and stops the rest.