3.2.7 Function Node
The Function node uses JavaScript code and allows you to construct your logic.
The message is passed in as an object named msg
. In general, the body of the message is present in the payload property msg.payload
of the msg object. The other nodes may attach their own properties to the message object.
Â
Function editor
You can enter custom code into the Function node and add return msg;
at the end to return the whole message to be passed on to the next node in the flow.
The function must always return a msg
object.
If the function returns null
, then no message is passed on through the function node.
Returning a number or string will result in an error.
Though it is not advised to rewrite the whole message object, you can construct a completely new object before returning it. For example:
var newMsg = { payload: msg.payload.length };
return newMsg;
Constructing a new message object will lose any message properties of the received message. This will break some flows, for example, the HTTP In/Response flow requires the msg.req
 and msg.res
properties to be preserved end-to-end.
You can click on the expand button to expand the code editor to fill the screen.
In the expanded mode, click on the Cancel button to return to the windowed mode.
Use node.warn()
to show warnings in the sidebar to help you debug. For example:
node.warn("my var xyz = " + xyz);
See the logging section below for more details.
Multiple Outputs
If there is more than one output, you can return an array of messages to the outputs using the function node. You can increase the number of outputs (also the pins of the function node) by changing the Outputs number (highlighted in the screenshot below).
In the Experience Designer, the function node with 3 outputs:
For example, this function would send anything on the topic apple
 to the second output rather than the first:
if (msg.topic === "apple") {
return [ null, msg ];
} else {
return [ msg, null ];
}
The following example passes the original message as-is on the first output and a message containing the payload length is passed to the second output:
Multiple Messages
A function can return multiple messages on the output by returning an array of messages within the returned array. When multiple messages are returned for an output, subsequent nodes will receive the messages one at a time in the order they were returned.
In the following example, msg1
, msg2
will be sent to the first output pin. msg3
 will be sent to the second output pin.
The following example splits the received payload into individual words and returns a message for each of the words.
Event logs
If a node needs to log something to the console, it can use one of the following functions:
The warn
 and error
 messages also get sent to the debug tab on the right side of the Experience Designer.
For finer-grained logging, node.trace()
 and node.debug()
 are also available. If there is no logger configured to capture those levels, they will not be seen.
Error Handling
If the function encounters an error that should halt the current flow, it should return nothing. To trigger a Catch node on the same tab, the function should call node.error
 with the original message as a second argument:
Storing data
Aside from the msg
 object, the function can also store data in the context store.
In the Function node there are three predefined variables that can be used to access context:
context
 - the node’s local contextflow
 - the flow scope contextglobal
 - the global scope context
The following examples use flow
 context but apply equally well to context
 and global
.
There are two modes for accessing context; either synchronous or asynchronous. The built-in context stores provide both modes. Some stores may only provide asynchronous access and will throw an error if they are accessed synchronously.
To get a value from context:
To set a value:
The following example maintains a count of how many times the function has been run:
Get/Set multiple values
It is also possible to get or set multiple values in one go:
Get
Set
In this case, any missing values are set to null
.
Status
The function node can also provide its own status decoration in the same way that other nodes can. To set the status, call the node.status
 function. For example
The shape
 property can be a ring
or dot
.
The fill
 property can be: red
, green
, yellow
, blue,
 or grey
If the status object is an empty object, {}
, then the status entry is cleared from the node.
Any status updates can then also be caught by the Status node.
Additional modules
Additional node modules cannot be loaded directly within a Function node. Refer, What external libraries and modules are allowed in Experience Designer? for supported modules and libraries.
API Reference
The following objects are available within the Function node.
node
node.id
 : the id of the Function nodenode.name
 : the name of the Function nodenode.log(..)
 : log a messagenode.warn(..)
 : log a warning messagenode.error(..)
 : log an error messagenode.debug(..)
 : log a debug messagenode.trace(..)
 : log a trace messagenode.on(..)
 : register an event handlernode.status(..)
 : update the node statusnode.send(..)
 : send a messagenode.done(..)
 : finish with a message
context
context.get(..)
 : get a node-scoped context propertycontext.set(..)
 : set a node-scoped context propertycontext.keys(..)
 : return a list of all node-scoped context property keyscontext.flow
 : same asÂflow
context.global
 : same asÂglobal
flow
flow.get(..)
 : get a flow-scoped context propertyflow.set(..)
 : set a flow-scoped context propertyflow.keys(..)
 : return a list of all flow-scoped context property keys
global
global.get(..)
 : get a global-scoped context propertyglobal.set(..)
 : set a global-scoped context propertyglobal.keys(..)
 : return a list of all global-scoped context property keys
env
env.get(..)
 : get an environment variable
Other modules and functions
The Function node also makes the following modules and functions available:
Buffer
 - the Node.jsÂBuffer
 moduleconsole
 - the Node.jsÂconsole
 module (node.log
 is the preferred method of logging)util
 - the Node.jsÂutil
 modulesetTimeout/clearTimeout
 - the javascript timeout functions.setInterval/clearInterval
 - the javascript interval functions.
Video Content
https://cdn.orbita.cloud/assets/orbita/experiencedesigner/functionnode.mp4
Useful Links
Related Content
Â