A JavaScript function 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.
The Setup tab contains code that will be run whenever the node is started. The Close tab contains code that will be run when the node is stopped.
If an promise object is returned from the setup code, input message processing starts after its completion.
See the online documentation for more information on writing functions.
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:
Note: The setup code is executed during the initialization of nodes. Therefore, if node.send
is called
in the setup tab, subsequent nodes may not be able to receive the message.
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.
To log any information, or report an error, the following functions are available:
node.log("Log message")
node.warn("Warning")
node.error("Error")
In the function block, the following information can be referenced:
node.id
- id of the nodenode.z
- id of the flownode.name
- name of the nodeHomegear RPC methods can be called with hg.invoke(methodName, [parameter1, parameter2, ...)
.
Data can be stored using
setNodeData(key, value)
,setFlowData(key, value)
orsetGlobalData(key, value)
and retrieved with
getNodeData(key)
,getFlowData(key)
orgetGlobalData(key)
.In addition Node-RED's context store API can be used as well.
Environment variables can be accessed using env.get("MY_ENV_VAR")
.