experienced programmer, inexperienced javascript programmer.
I am working with Node-RED and in one of my nodes, I am calling an external program with 'child_process' which sends data back to my node via stdout.
I am using console.info(stdout) to log the child process data, so I know that it is working appropriately. This is the code within the function node:
/* required modules */
var cp = context.global.child_process;
/* variables */
var packet = {};
var cmd = '~/packet/packet.py ';
/* construct the packet object */
packet.networkTime = (msg.payload[3] << 24) + (msg.payload[2] << 16) + (msg.payload[1] << 8) + msg.payload[0];
packet.sampleCount = msg.payload[4];
packet.uncompressedWidth = msg.payload[5];
packet.compressedWidth = msg.payload[6];
packet.sample0 = (msg.payload[8] << 8) + msg.payload[7];
var compressedData = msg.payload.slice(9);
/* change buffer type to array */
packet.compressedData = [];
compressedData.forEach(
function(element){
packet.compressedData.push(element);
}
);
/* stringify the object */
var packet_str = "'" + JSON.stringify(packet) + "'";
/* this section will execute the command */
cp.exec(
cmd + packet_str,
function(error, stdout, stderr){
console.info(stdout);
return JSON.parse(stdout);
}
);
//return data_msg;
I tried setting data_msg to {} and using JSON.parse(stdout) to place into that object, but the 'return data_msg' always executes before the value is returned. I simply want to wait for my external process to return data before returning this node.
Thank you for your assistance,