Long code in function node...how to wrap

Noob question here....I have a very long string of code within a function node that is difficult to read and cannot figure out how to "wrap" the code inside the window without breaking the functionality. For instance...msg.topic is hundreds of characters in length and I am getting confused by using the scroll bar and would like to format the window for a more plessant means to check for syntax errors etc.

Can someone please provide the characters/syntax for helping me achieve this? Thanks in advance!

Why do you need such a long topic?

It is an insert statement into a SQL database:

msg.topic="INSERT INTO electrodacus (SOC,cell_1,cell_2,cell_3,cell_4,Batt_Amps,Ext_Load) VALUES ("+msg.payload.soc+""+msg.payload.cell1+","+msg.payload.cell2+","+msg.payload.cell3+","+msg.p```


etc....etc...etc...

Use template literals...

msg.topic=`
INSERT INTO electrodacus 
(SOC,cell_1,cell_2,cell_3,cell_4,Batt_Amps,Ext_Load) 
VALUES
(${msg.payload.soc}, ${msg.payload.etc}, ...
`
1 Like

What database? Mysql? MSSQL? Postgres?

I ask because the better/safer way to build a query is to use a parametrised query.

MYSQL and this is an insert statement

I have tried various iterations of using backticks but get an error of either "ER_WRONG_VALUE_COUNT_ON_ROW"

With this

msg.topic=
`INSERT INTO electrodacus
(SOC,cell_1,cell_2,cell_3,cell_4,Batt_Amps,Ext_Load)
VALUES
("+msg.payload.soc+""+msg.payload.cell1+","+msg.payload.cell2+","+msg.payload.cell3+","+msg.payload.cell4+","+msg.payload.battAmps+","+msg.payload.extLoad+")`
msg.payload=[msg.payload.soc,msg.payload.cell1,msg.payload.cell2,msg.payload.cell3,msg.payload.cell4,msg.payload.battAmps];
return msg;

Or

ER_PARSE_ERROR
with this

msg.topic=
`"INSERT INTO electrodacus
(SOC,cell_1,cell_2,cell_3,cell_4,Batt_Amps,Ext_Load)
VALUES
("+msg.payload.soc+""+msg.payload.cell1+","+msg.payload.cell2+","+msg.payload.cell3+","+msg.payload.cell4+","+msg.payload.battAmps+","+msg.payload.extLoad+")"`
msg.payload=[msg.payload.soc,msg.payload.cell1,msg.payload.cell2,msg.payload.cell3,msg.payload.cell4,msg.payload.battAmps];
return msg;

I missed the $ sign and corrected:

msg.topic=`
INSERT INTO electrodacus
(SOC,cell_1, cell_2, cell_3, cell_4, Batt_Amps,Ext_Load)
VALUES
(${msg.payload.soc}, ${msg.payload.cell1}, ${msg.payload.cell2}, ${msg.payload.cell3}, ${msg.payload.cell4}, ${msg.payload.battAmps}, ${msg.payload.extLoad$}
`
msg.payload=[msg.payload.soc,msg.payload.cell1,msg.payload.cell2,msg.payload.cell3,msg.payload.cell4,msg.payload.battAmps];
return msg;

But now get this error

"Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 4"

I have to watch my typing...had and extra $, removed and working...thank you so very much!!

msg.topic=`
INSERT INTO electrodacus
(SOC,cell_1, cell_2, cell_3, cell_4, Batt_Amps,Ext_Load)
VALUES
(${msg.payload.soc}, ${msg.payload.cell1}, ${msg.payload.cell2}, ${msg.payload.cell3}, ${msg.payload.cell4}, ${msg.payload.battAmps}, ${msg.payload.extLoad})
`
msg.payload=[msg.payload.soc,msg.payload.cell1,msg.payload.cell2,msg.payload.cell3,msg.payload.cell4,msg.payload.battAmps];
return msg;

I am curious about this as well. I 'see' the syntax, but I do not 'understand' the syntax.

Can someone please explain the ` and the $ and the {}s ? And their function/use?

Thanks

Joe

Look up 'Template Literals'

A simple code example should explain it...

const myName = " Joe";
const aStringVar = `Hello ${myName}`;
console.log(aStringVar);

//prints:    Hello Joe 

This example ↑ is a bit simplistic but should spell out what is happening.


Here ↓ is another example to bolster the notion that inside of ${} is actually being evaluated...

const ninetyNine = 99;
function toHexadecimal(v) {
  return v.toString(16);
}
const result = `${ninetyNine + 1} decimal equals 0x${toHexadecimal(ninetyNine + 1)} hexadecimal`;
console.log(result);

//prints:   100 decimal equals 0x64 hexadecimal

This makes building strings with lots of dynamic values much more manageable.

1 Like

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