Search code examples
javascriptnode.jsnode-red

insert two different msg.payload in a database


I want to do the query below into a function: insert into table values (msg.payload.1, msg.payload.2);

this function has two input (2 different msg.payload) so the code that I am trying to write is:

var m={
    topic: "insert into info values ('"+msg.payload+"','"+msg.payload+"');"
};
return m;

it is adding two rows in the info table. but want to add only one row with two different values.

any idea?

Thanks in advance


Solution

  • If I've understood properly you have 2 different nodes feeding data into this function and you want to use the values from these separate messages to build your insert statement.

    This is not how Node-RED works, each message is handled totally separately from each other (hence why you see 2 rows added to the DB).

    If you want to combine 2 separate messages you will need to make use of the context to keep state. You will also need a way to identify which message is which when it comes in, this is normally done by message topic, but it could be any unique feature.