# Using the default node names feature in NR 3.0

**URL:** <https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021>\
**Category:** Developing Nodes\
**Created:** [11 February 2023 00:08 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021 "2023-02-11T00:08:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 February 2023 00:08 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021/1 "2023-02-11T00:08:48Z")

</div>

I would like to use the default node naming [process](https://nodered.org/blog/2022/07/14/version-3-0-released#default-node-names) introduced in NR version 3.0 in one of my contributed nodes. I also want to leverage it to define another node property (`outputTopic`) in the code below.

```auto
onadd: function() {
    if (RED.settings.version.split('.')[0] >= 3 && this.name === "") {
        RED.actions.invoke("core:generate-node-names", this, {generateHistory: false})
        this.outputTopic = this.name
    }
}

```

This works perfectly when I drag an instance of the node from the palette, but if I use copy-and-paste to duplicate a node already in the workspace, the copy gets a new name, but it has the same `outputTopic`. I was surprised that copying worked at all, but is there something I can do get the other property too?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 February 2023 01:24 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021/2 "2023-02-11T01:24:42Z")

</div>

I think because the copy already has a name - copied from the original - so it isn't going into the if statement?

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 February 2023 03:30 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021/3 "2023-02-11T03:30:57Z")

</div>

Of course. 🙂 That's right. But then the editor knows to rename the copy, and it must be tracking nodes of that type so it can keep on renaming them. I'll have to come up with another trick to cover the case.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 February 2023 09:56 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021/4 "2023-02-11T09:56:23Z")

</div>

The debug node does it slightly differently:

> <https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/nodes/core/common/21-debug.html#L551-L555>

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 February 2023 11:16 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021/5 "2023-02-11T11:16:11Z")

</div>

Yes, that was my starting point. Since `debug` is part of the core, I assumed (without checking) that this code was not present in previous versions of NR. Hence, my test for version number. The test for `_DEFAULT_` works because that is how `name` is defined in the `defaults` object. I realize now that it works to determine whether the node instance has been dragged from the palette or pasted into the workspace. So, my requirements are: do no harm in earlier versions of NR; allow definition of properties in addition to `name`; and work properly for nodes pasted into the workspace. I have ideas, but I'm not quite there yet.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 February 2023 16:40 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021/6 "2023-02-11T16:40:25Z")

</div>

Why not use the same naming - make the default `_DEFAULT_` and the node should do the right thing when it is added to a flow shouldn't it? All you need to do in the onAdd part is add the test for the Node-RED version along with testing for the default name. Or have I missed something obvious?

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [11 February 2023 21:13 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021/7 "2023-02-11T21:13:03Z")

</div>

Just a couple of loose ends. Apparently, calling `RED.actions.invoke` does no damage in older versions of NR, so testing for the version number may not be necessary. I was not sure whether `onadd` is executed in the copy-and-paste case. Since it is, `this.outputTopic = this.name` can be moved outside the `if`, and everything works. If this survives a bit more testing, I'm done.

---

<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:** [25 February 2023 21:13 UTC](https://discourse.nodered.org/t/using-the-default-node-names-feature-in-nr-3-0/75021/8 "2023-02-25T21:13:50Z")

</div>

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