I am trying to achieve to read from a DB2 table and insert to a different DB2 table using node-red. For this i am using dashdb modules(https://flows.nodered.org/node/node-red-nodes-cf-sqldb-dashdb). I have made sure that columns are matching in both source and target and tested to insert multiple records in target table using select query as select * from source_table and then insert query for the insert table. Now i have another thing in which i need to fire multiple select queries on source table and insert the result records in target table asynchronously. All the multiple select queries would be identical except where id = ? . So i tried to put the query in msg.payload inside a for loop like this
var queryID = 2; // max number of id to be querying against
for (i=1; i<=queryID; i++) {
console.log("COUNT", i)
msg.payload = `select * from source_table where id= ${i}` // only i changes
node.send(msg); // i pass it to next node which is dashBD out node responsible for selecting record from source table
}
But doing this only executes the last loop value which is 2 and do not node.send the value where i = 1 to the next node.
Can anyone shed some light here: It inserts the result with id=2 two times in the target table. Instead i want to execute insert according to the for loop i variable. This is the second node module function in the graph
module.exports = function(RED) {
function fireMultipleQuery(nodeConfig) {
RED.nodes.createNode(this,nodeConfig);
var node = this;
node.on('input', function(msg) {
var queryID = 2; // max number of id to be querying against
for (i=1; i<=queryID; i++) {
console.log("COUNT", i)
msg.payload = `select * from source_table where id= ${i}` // only i changes
node.send(msg); // i pass it to next node which is dashBD out node responsible for selecting record from source table
});
}
RED.nodes.registerType("fire-query-node", fireMultipleQuery);
}
NodeJS is a pass by reference language.
So because the loop is updating the same msg
object it is changing the already sent version as well.
You will probably want to clone the msg
or create a new object for each send.
You can clone messages with RED.util.cloneMessage()