I have a function in a node that searches for and returns the line of a text file. It does what it should, but the output is only assigned to the variable inside the function. Additionally, the debug output seems to indicate that the line after I call the function is being called first. I've tried all sorts of things the past several hours but can't put my finger on what the problem is.
Thank you,
Chris
function Get_Message(File, msgNumber){
// Check if the file exists.
if (parseInt(msgNumber) != 0) {
fs.access(File, fs.F_OK, (err) => {
if (err) {
node.warn(err);
}
// File exists.
var line = msgNumber - 1;
var myInterface = readline.createInterface({
input: fs.createReadStream(File)
});
var lineno = 0;
myInterface.on('line', function (line) {
lineno++;
if (lineno == msgNumber){
var Message = line;
node.warn(Message)
}
});
})
}
return Message;
}
I'm deciding which file just before I call the function. It seems far more efficient to read the file at the time I need it and continue processing from where I'm at, rather than daisy-chaining even more nodes.
Fair enough. I did what you suggested and am reading the file. I now have a rather large string I need to parse. I was looking to grab a specific line from the file.
Thank you. I used a function to set my filename and combine the results into an array. I had to create a global variable in the same function to get the original msg.payload to the end function. All set. Thanks, guys!
if you needed to pass the original payload further down the flow line ..
since you used a function .. you could simply transfer the original payload to another msg property to protect it .. because yes, the payload would have been replaced by the file-in node's output.
msg.originalPayload = RED.util.cloneMessage(msg.payload) [EDIT] we clone the msg with NR util to avoid by reference
I ended up doing most of the processing in the first node and assigning everything to a new object that I made global. All worked out well. Thank you! I appreciate it!