# Flow variable not accessible within assigned string

**URL:** https://discourse.nodered.org/t/flow-variable-not-accessible-within-assigned-string/37082
**Category:** General
**Created:** [6 December 2020 14:57 UTC](https://discourse.nodered.org/t/flow-variable-not-accessible-within-assigned-string/37082 "2020-12-06T14:57:56Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![CreativeWarlock](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/creativewarlock/32/102051_2.png) [@CreativeWarlock](https://discourse.nodered.org/u/CreativeWarlock)
#### Post date: [6 December 2020 14:57 UTC](https://discourse.nodered.org/t/flow-variable-not-accessible-within-assigned-string/37082/1 "2020-12-06T14:57:56Z")

</div>

Hey guys,

I've written a code block inside a function which should read a flow variable called selected\_symbol and writes out a msg.topic. I have made sure that selected\_symbol is properly filled with a string (e.g. "IDR.ES"). Still the function only returns an empty msg.topic:

```auto
msg.topic = `INSERT INTO TRADE_DATA.ORDERS (SYMBOL, CREATED_TIME) 
    VALUES(
        \"` & flow.get('selected_symbol') & `\",
        FROM_UNIXTIME(UNIX_TIMESTAMP())
    ");`

node.error("Error! msg.topic = " & msg.topic);

return msg;

```

As mentioned node.error(..) returns "Error! msg.topic = " - that's it.

(For brevity I'm highlighting only a shortened insert statement).  
Also, for this string I'm using ` characters in order to be flexible when using ' or " in my SQL statement.

I have tried with ${flow.selected\_symbol} and $flowContext('selected\_symbol') but neither are recognized.

Can anyone enlighten me what I'm doing wrong?

---

<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: [6 December 2020 15:23 UTC](https://discourse.nodered.org/t/flow-variable-not-accessible-within-assigned-string/37082/2 "2020-12-06T15:23:11Z")

</div>

because your string is a template literal you dont need to escape `"` also, as you are using a template litteral, you should use `${var}` syntax e.g...

try

```auto
msg.topic = `INSERT INTO TRADE_DATA.ORDERS (SYMBOL, CREATED_TIME) 
    VALUES(
        "${flow.get('selected_symbol')}", 
        FROM_UNIXTIME(UNIX_TIMESTAMP())
    ");`
node.warn(msg.topic);

```

---

<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: [6 December 2020 15:25 UTC](https://discourse.nodered.org/t/flow-variable-not-accessible-within-assigned-string/37082/3 "2020-12-06T15:25:57Z")

</div>

The first problem is the in javascript the operator `&` is the bitwise AND operator not string concatenation. That is `+`. Also since you are using backticks you shouldn't be escaping the double quotes, I don't think. It would be more conventional to use single quotes for the strings and again no need to escape the double quotes.

---

<div class="post-metadata">

### Author: ![CreativeWarlock](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/creativewarlock/32/102051_2.png) [@CreativeWarlock](https://discourse.nodered.org/u/CreativeWarlock)
#### Post date: [6 December 2020 17:33 UTC](https://discourse.nodered.org/t/flow-variable-not-accessible-within-assigned-string/37082/4 "2020-12-06T17:33:04Z")

</div>

Thanks a bunch, mate!

Adding ${...} was the key!

Also, thanks for pointing out that escaping " is not necessary.

---

<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: [20 December 2020 17:33 UTC](https://discourse.nodered.org/t/flow-variable-not-accessible-within-assigned-string/37082/5 "2020-12-20T17:33:06Z")

</div>

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