The Function node uses JavaScript code and allows you to construct your logic for the flow.
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.
...
You can enter custom code into the Function node and add return msg;
at the end to return the whole msg message to be passed on to the next node in the flow.
...
If there is more than one output, you can return an array of messages can be returned by the function to send 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:
...
This makes it easy to write a function that sends the message to different outputs depending on some conditions. For example, this function would send anything on the topic banana
apple
to the second output rather than the first:
Code Block |
---|
if (msg.topic === "bananaapple") { 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:
Code Block |
---|
msg.topic = "apple" var newMsg = { payload: msg.payload.length }; return [msg, newMsg]; |
Multiple Messages
...
In the following example, msg1
, msg2
, msg3
will will be sent to the first output pin. msg4
msg3
will be sent to the second output pin.
Code Block |
---|
var msg1 = { payload:"first outoutput of outputpin 1" }; var msg2 = { payload:"second outoutput of outputpin 1" }; var msg3 = { payload:"third out of output 1" }; var msg4 = { payload:"only message from outputpin 2" }; return [ [ msg1, msg2, msg3 ], msg4msg3 ]; |
The following example splits the received payload into individual words and returns a message for each of the words.
...
Code Block |
---|
node.log("Something happenedto be logged"); node.warn("Something happenedto youwarn shouldthe know aboutuser"); node.error("Oh no, something bad happenedRed Alert!!!"); |
The warn
and error
messages also get sent to the debug tab on the right side of the Experience Designer.
...
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:
Code Block |
---|
node.error("You have hit an error", msg); |
Storing data
...
Code Block |
---|
flow.set(["count", "colour", "temperature"], [123, "red", "1236.5"]); |
In this case, any missing values are set to null
.
...
Code Block |
---|
node.status({fill:"red",shape:"ring",text:"disconnected"}); node.status({fill:"green",shape:"dot",text:"connected"}); node.status({text:"Just text status"}); |
The shape
property can be: be a ring
or 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.
...
The function node automatically clears any outstanding timeouts or interval timers whenever it is stopped or re-deployed.
...
.
Related Articles
Filter by label (Content by label) | ||||||||
---|---|---|---|---|---|---|---|---|
|