# Asking about lookup structure / allowed characters

**URL:** <https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043>\
**Category:** General\
**Created:** [30 April 2023 11:06 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043 "2023-04-30T11:06:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 April 2023 11:06 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/1 "2023-04-30T11:06:55Z")

</div>

I'm asking here because if I don't ask, I won't get an answer.  
Yes, I can test it myself but going on past history for me I still need to ask.  
_(Otherwise I would do just that and it will fail so I have to ask all the same.)_

Code:

```auto
const channels = flow.get("channels");

let x = channels[msg.payload].number;

//msg.payload = x.number;
msg.payload = x;

return msg;

```

This is an exert from the `flow"chanels"`:  
(ok, from the `function` node that sets it.)

```auto
    "bold": { "number": 166 },
    "peach": { "number": 167 },
    "foxone+2": { "number": 201 },
    "abcHD": { "number": 202 },

```

This is because I **HAD** the last one _named_ as `abd-HD` rather than `abcHD`.  
The `-` sign somehow confused the lookup.

So my question stands will the `+` will do the same?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 April 2023 11:35 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/2 "2023-04-30T11:35:32Z")

</div>

# UPDATE:

Ok, I stuffed up.

So now the _problem_ is:  
How can I _catch_ the error if the channel name is not found?  
In that `function` node.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 April 2023 12:02 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/3 "2023-04-30T12:02:06Z")

</div>

Hi @Trying_to_learn  
You can apply Optional Chaining to stop an exception.

> **[Optional chaining (?.) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)**
>
> The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an...

```js
let x = channels[msg.payload]?.number; /* x will be undefined, if the key is not found */
if(x !== undefined){
   // do something with x
}

```

I have used `!== undefined` as 0 can be falsey, so if you happen to have a number of 0, it will evaluate to false, so instead ACTUALLY check if the type is `undefined`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 April 2023 12:05 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/4 "2023-04-30T12:05:45Z")

</div>

Thanks.

But that doesn't address the problem if I send it a channel name that isn't in the list.

I have (been digging) and this seems to work.

```auto
const channels = flow.get("channels");

const check = channels[msg.payload];
if (check == undefined) throw("Channel not found");

let x = channels[msg.payload].number;
//msg.payload = x.number;
msg.payload = x;

return msg;

```

You only just beat me to the reply.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 April 2023 12:08 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/5 "2023-04-30T12:08:28Z")

</div>

Yup, what I put is basically the same thing, just slightly shorter,

```auto
let x = channels[msg.payload]?.number; /* x will be undefined, if the key is not found */
if(x !== undefined){
   // do something with x
}
else{
   throw("Channel not found");
}

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 April 2023 12:08 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/6 "2023-04-30T12:08:37Z")

</div>

Oh, sorry...

What you said there MAY work for what I want.

I am still not understanding all the _short hand_ writing of `?.` and what you did.

I'll look into that structure and see if I can remember that for next time.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 April 2023 12:10 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/7 "2023-04-30T12:10:38Z")

</div>

I'll slightly re-do it so the _fail_ is the first part.

I'll put it in the code so I see it and it may prompt me to use it next time.  
Again Thanks.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 April 2023 12:12 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/8 "2023-04-30T12:12:56Z")

</div>

No worries

`?` in this context, will check if there is a value to continue with, if there is not, return `undefined`

```auto
channels["Non-existing-key"].number // exception
channels["Non-existing-key"]?.number // undefined

```

> **[Optional chaining (?.) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)**
>
> The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an...

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 April 2023 12:14 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/9 "2023-04-30T12:14:12Z")

</div>

I have that site book marked - in the bigger picture.

Now I have the fun job of learning all those _tricks_.  
🙂

---

<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:** [14 May 2023 12:14 UTC](https://discourse.nodered.org/t/asking-about-lookup-structure-allowed-characters/78043/10 "2023-05-14T12:14:37Z")

</div>

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