# Accessing node name e.g. in a function

**URL:** <https://discourse.nodered.org/t/accessing-node-name-e-g-in-a-function/61863>\
**Category:** General\
**Tags:** function-node\
**Created:** [27 April 2022 10:18 UTC](https://discourse.nodered.org/t/accessing-node-name-e-g-in-a-function/61863 "2022-04-27T10:18:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![binderth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/binderth/32/35124_2.png) [@binderth](https://discourse.nodered.org/u/binderth)\
**Post date:** [27 April 2022 10:18 UTC](https://discourse.nodered.org/t/accessing-node-name-e-g-in-a-function/61863/1 "2022-04-27T10:18:59Z")

</div>

To get a better glance, I use multiple function nodes for lets say JSON-attributes and go on with them.  
In my specific use case, I pick some special attributes and send them to distinct MQTT-topics.  
So far, so good, all my nodes look somehing like this:

```auto
// JSON parsen
parsed = JSON.parse(msg.payload);
msg.payload = parsed.vehicleStatus.evStatus.batteryStatus;
msg.topic = "batterySoC";
return msg;

```

what would make life a bit easier would be, if the `mgs.topic` could get its value from the name of the Node, so something like

```auto
msg.topic = NODE_NAME;

```

Because then I'd only have to name the nodes and the MQTT-topic and -message would be in `msg` as it should be?

see:

> **Node-Export**
>
> ```auto
> [
> {
> "id": "260f4b90e1219457",
> "type": "function",
> "z": "a351eaa.aed7b98",
> "name": "batteryStatus",
> "func": "// JSON parsen\nparsed = JSON.parse(msg.payload);\nmsg.payload = parsed.vehicleStatus.evStatus.batteryStatus;\nmsg.topic = \"batterySoC\"\nreturn msg;\n",
> "outputs": 1,
> "noerr": 0,
> "initialize": "",
> "finalize": "",
> "libs": [],
> "x": 840,
> "y": 320,
> "wires": [
> []
> ]
> }
> ]
> 
> ```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 April 2022 10:27 UTC](https://discourse.nodered.org/t/accessing-node-name-e-g-in-a-function/61863/2 "2022-04-27T10:27:03Z")

</div>

try...

```javascript
msg.topic = env.get("NR_NODE_NAME");

```

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [27 April 2022 12:59 UTC](https://discourse.nodered.org/t/accessing-node-name-e-g-in-a-function/61863/3 "2022-04-27T12:59:08Z")

</div>

or

`msg.topic = node.name;`

... that is little easier to remember 😉

---

<div class="post-metadata">

**Author:** ![binderth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/binderth/32/35124_2.png) [@binderth](https://discourse.nodered.org/u/binderth)\
**Post date:** [27 April 2022 13:11 UTC](https://discourse.nodered.org/t/accessing-node-name-e-g-in-a-function/61863/4 "2022-04-27T13:11:24Z")

</div>

works now. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 May 2022 13:12 UTC](https://discourse.nodered.org/t/accessing-node-name-e-g-in-a-function/61863/5 "2022-05-11T13:12:19Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
