Template node - node.name

In a function node, one can use node.name to get the current node name, would it be possible to have this exposed/evaluated in a template node as well ?

eg.

All the node's config parameters are available in the template node's props object.
for example:

this.send(this.props.name); // outputs the node name

Omri, the OP is asking about the Node-RED core template (not ui-template)

@bakman2 You already have access to the same ENV VARs the function node does:

This is the payload: {{payload}} 

env.NR_FLOW_NAME: {{env.NR_FLOW_NAME}}

env.NR_GROUP_NAME: {{env.NR_GROUP_NAME}}

env.NR_NODE_NAME: {{env.NR_NODE_NAME}}

5 Likes

:astonished_face: when did that happen?! I must have been asleep when that was introduced. :smile:

Also, is there a way to find all of the env variables? As I note that they are not included in process.env.

THere also seems to be a bug in the env.get() function. If you try to use env.NR_NODE_NAME in a function node and the node does not have a name set, it throws an error and so stops the function from executing. It should not do that, having an undefined name is perfectly valid and not an error condition.

That is true for other env variables such as group name and id as well so presumably it is a problem with the get function.

works fine here
chrome_UULNEJCUgu

Try this. BTW, I'm using the Beta version of Node-RED

// msg.payload1 = process.env
msg.payload = {
    NR_NODE_ID: env.NR_NODE_ID // - the ID of the node
    // NR_NODE_NAME: env.NR_NODE_NAME // - the Name of the node
    NR_NODE_PATH: env.NR_NODE_PATH // - the Path of the node. This represents a node’s position in a flow. It is / delimited IDs of the flow, enclosing subflows, and the node.
    // NR_GROUP_ID: env.NR_GROUP_ID // - the ID of the containing group
    // NR_GROUP_NAME: env.NR_GROUP_NAME // - the Name of the containing group
    // NR_FLOW_ID: env.NR_FLOW_ID // - the ID of the flow the node is on
    // NR_FLOW_NAME: env.NR_FLOW_NAME // - the Name of the flow the node is on
    // NR_SUBFLOW_NAME: env.NR_SUBFLOW_NAME // - the Name of the containing subflow instance node (since Node-RED 3.1)
    // NR_SUBFLOW_ID: env.NR_SUBFLOW_ID // - the ID of the containing subflow instance node (since Node-RED 3.1)
    // NR_SUBFLOW_PATH: env.NR_SUBFLOW_PATH // - the Path of the containing subflow instance node (since Node-RED 3.1)
}
return msg;

but that is not how env is supposed to be used.

From online docs: Writing Functions : Node-RED

env

  • env.get(..) : get an environment variable

And the function signature:

This might be another bug in the beta as well? Note the lack of Monaco icons:

BTW, this is a clean, standard global install of Node-RED, not my usual install.

And this also fails:

// msg.payload1 = process.env
msg.payload = {
    NR_NODE_ID: env.get('NR_NODE_ID') // - the ID of the node
    NR_NODE_NAME: env.get('NR_NODE_NAME') // - the Name of the node
    // NR_NODE_PATH: env.get('NR_NODE_PATH') // - the Path of the node. This represents a node’s position in a flow. It is / delimited IDs of the flow, enclosing subflows, and the node.
    // NR_GROUP_ID: env.get('NR_GROUP_ID') // - the ID of the containing group
    // NR_GROUP_NAME: env.get('NR_GROUP_NAME') // - the Name of the containing group
    // NR_FLOW_ID: env.get('NR_FLOW_ID') // - the ID of the flow the node is on
    // NR_FLOW_NAME: env.get('NR_FLOW_NAME') // - the Name of the flow the node is on
    // NR_SUBFLOW_NAME: env.get('NR_SUBFLOW_NAME') // - the Name of the containing subflow instance node (since Node-RED 3.1)
    // NR_SUBFLOW_ID: env.get('NR_SUBFLOW_ID') // - the ID of the containing subflow instance node (since Node-RED 3.1)
    // NR_SUBFLOW_PATH: env.get('NR_SUBFLOW_PATH') // - the Path of the containing subflow instance node (since Node-RED 3.1)
}
return msg;

There is no comma after NR_NODE_ID: env.get('NR_NODE_ID')

Kind of goes against JavaScript standards where a get function should be transparent. However.

In the beta, I am not getting the function signature in Monaco.

Oh, wait - no, I wondered if it was somehow set to ACE but no, it is set to Monaco.

Not seeing that at all - I JUST installed node-red@next

chrome_z8keszmquR

Not a standard I've read? Perhaps you are thinking of how getter and setters props are written? (in this case get is a method)

Darn it! I'm on an annoying little MS Surface pro, not my home setup! But also annoying that while the syntax error did show in the NR log, it did not show in NR Admin.

Thanks for correcting that, this now works:

// msg.payload1 = process.env
msg.payload = {
    NR_NODE_ID: env.get('NR_NODE_ID'), // - the ID of the node
    NR_NODE_NAME: env.get('NR_NODE_NAME'), // - the Name of the node
    NR_NODE_PATH: env.get('NR_NODE_PATH'), // - the Path of the node. This represents a node’s position in a flow. It is / delimited IDs of the flow, enclosing subflows, and the node.
    NR_GROUP_ID: env.get('NR_GROUP_ID'), // - the ID of the containing group
    NR_GROUP_NAME: env.get('NR_GROUP_NAME'), // - the Name of the containing group
    NR_FLOW_ID: env.get('NR_FLOW_ID'), // - the ID of the flow the node is on
    NR_FLOW_NAME: env.get('NR_FLOW_NAME'), // - the Name of the flow the node is on
    NR_SUBFLOW_NAME: env.get('NR_SUBFLOW_NAME'), // - the Name of the containing subflow instance node (since Node-RED 3.1)
    NR_SUBFLOW_ID: env.get('NR_SUBFLOW_ID'), // - the ID of the containing subflow instance node (since Node-RED 3.1)
    NR_SUBFLOW_PATH: env.get('NR_SUBFLOW_PATH'), // - the Path of the containing subflow instance node (since Node-RED 3.1)
}
return msg;

Are you able to install NR with a --production flag, not sure if that is causing it. I tend to do that without thinking.

Might just be that I have had to get up close and personal with a lot of getters/setters recently and expect that a something.get('fred') === something.fred since this is an increasingly common thing to do.

And, to be fair - that is how you wrote it in your original post! :smile: But yes, I realise that is a different case - however, it is very confusing for users.

This works for me, thanks. I am not able to close this topic (?). Perhaps someone can mark it ?