A JavaScript function block to run against the messages being received by the node.

The messages are passed in as a JavaScript object called msg.

By convention it will have a msg.payload property containing the body of the message.

The function is expected to return a message object (or multiple message objects), but can choose to return nothing in order to halt a flow.



See the online documentation for more information on writing functions.

Sending messages

The function can either return the messages it wants to pass on to the next nodes in the flow, or can call node.send(messages).

It can return/send:

  • a single message object - passed to nodes connected to the first output
  • an array of message objects - passed to nodes connected to the corresponding outputs

If any element of the array is itself an array of messages, multiple messages are sent to the corresponding output.

If null is returned, either by itself or as an element of the array, no message is passed on.

Logging and Error Handling

To log any information, or report an error, the following functions are available:

  • node.log("Log message")
  • node.warn("Warning")
  • node.error("Error")

The Catch node can also be used to handle errors. To invoke a Catch node, pass msg as a second argument to node.error:

node.error("Error",msg);

Accessing Node Information

In the function block, id and name of the node can be referenced using the following properties:

  • node.id - id of the node
  • node.name - name of the node

Using environment variables

Environment variables can be accessed using env.get("MY_ENV_VAR").