# Use an object with a hyphen in a function

**URL:** <https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257>\
**Category:** General\
**Created:** [16 April 2019 18:37 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257 "2019-04-16T18:37:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbomacfly](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gbomacfly/32/6568_2.png) [@gbomacfly](https://discourse.nodered.org/u/gbomacfly)\
**Post date:** [16 April 2019 18:37 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/1 "2019-04-16T18:37:10Z")

</div>

Hello all,

I have a problem with a function in node-red. I want to read an object of the instance spotify-premium. The problem is the hyphen in the name.  
The path I would like to read is:

```auto
spotify-premium.0.player.isPlaying

```

So I use:

```auto
if (spotify-premium.0.player.isPlaying === true) {
    msg.payload = true;
} else {
    msg.payload = false;
}
return msg; 

```

This doesn't work:

```auto
A leading decimal point can be confused with a dot...

```

So I tried

```auto
if (spotify-premium[0].player.isPlaying === true) { ...

```

But it gives me an error:

```auto
"ReferenceError: spotify is not defined (line 2, col 1)"

```

I also tried

```auto
if (spotify-premium[0].player.isPlaying === true) { ...

```

This gives me

```auto
"TypeError: Cannot read property 'isPlaying' of undefined"

```

Where is my problem? What have I done wrong?  
Thanks in forward 🙂

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [16 April 2019 19:14 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/2 "2019-04-16T19:14:44Z")

</div>

> [@gbomacfly](#):
>
> The problem is the hyphen in the name.

You already know the root cause: hyphen cannot be used in JavaScript identifiers.

Is there any reason that prevents you from using another identifier?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [16 April 2019 19:30 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/3 "2019-04-16T19:30:27Z")

</div>

For your own created properties it is just easier to avoid hyphens, but if incoming data is not under your control you have option to use [bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors)  
.

```auto
spotify-premium.0.player.isPlaying 

["spotify-premium"].0.player.isPlaying

```

---

<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:** [16 April 2019 19:52 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/4 "2019-04-16T19:52:31Z")

</div>

Wouldn't the 0 also be illegal?

`["spotify-premium"]["0"].player.isPlaying`

Also, not certain how there is a base object with that name?

Surely it's something like  
`msg["spotify-premium"]["0"].player.isPlaying`?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [16 April 2019 20:18 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/5 "2019-04-16T20:18:30Z")

</div>

Yeah, keep things simple, dont do this  
`msg.nastyobject = {"0__foo-bar":{"1|2":{"a:-b":"evil"}}}`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 April 2019 21:09 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/6 "2019-04-16T21:09:25Z")

</div>

If in doubt about how to reference something send it to a debug node then one of the little buttons that appears when you hover over it, when clicked, will copy the path, which you can paste somewhere and look at.

---

<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:** [16 April 2019 21:44 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/7 "2019-04-16T21:44:24Z")

</div>

That feature is very useful however I wish it would suffix it with `msg.` 🙂

---

<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:** [16 April 2019 21:45 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/8 "2019-04-16T21:45:09Z")

</div>

Oh and I wish they had tooltips as I never know which is which 🤔

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 April 2019 21:53 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/9 "2019-04-16T21:53:07Z")

</div>

> [@Steve-Mcl](#):
>
> That feature is very useful however I wish it would suffix it with `msg.`

It doesn't include `msg.` so the value can be pasted straight into a Change node (or any other TypedInput field).

Decided, on balance, that was more convenient.

---

<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:** [16 April 2019 22:19 UTC](https://discourse.nodered.org/t/use-an-object-with-a-hyphen-in-a-function/10257/10 "2019-04-16T22:19:30Z")

</div>

I did suspect that but I typically (or rather untypically ) tend to not use the likes of change nodes and more so use function nodes. But I do appreciate why.
