Flow variable not accessible within assigned string

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:

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?

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

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

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.

Thanks a bunch, mate!

Adding ${...} was the key!

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

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