I have a file with filenames that I need to process one-by one.
So I use a read-file-node to read that file, and write out one record at a time. Behind this there is a series of other nodes to process the filename and create an output.
My problem lies in the processing: I use global variables for several purposes, and they are mixed up when I have more than one entry in the original file. When I process one record only, it runs OK, but as soon as I remove that limit nothing sensible comes out.
What I would like to do is process one record at a time, and take up the next one when the whole thing is finished for the previous one.
How can this be done?
There could be 1 or several issues with your flow. Hard to tell without additional detail.
The 1st thing to remember (sorry if you already know this) is that dealing with flows, things may not always happen in the expected order or timing. Especially so when dealing with the filing system.
This can result in timing issues which I think you might be seeing here.
There are a couple of things you might find helpful. One might be not to use global variables but rather function nodes with local context variables. But perhaps more likely is that you need your variables to be an array or object capable of holding details of multiple files.
For that 2nd case, initialise the global as an object. Then, when storing file details, store it to myGlobalVar[filename]. Do make sure that you have something that removes the details again though if you don't want things piling up until Node-RED restarts (unless you want that).
Again, hard to give advice without more detail. Perhaps a screen-shot of your flow at least might help.
But if you really want to process things this way, you may be better off using a Finite State Machine node - several are available - with a gate that holds further processing until the last has finished.
Watch out though. This approach can be fraught with edge-cases like running out of memory or slowing everything else to a crawl.
It is much better I think to rework your flow to deal with multiple records.
More information on the overall process, what your final aim is and what the data looks like will give us a chance to give you better advice.
Rather than global variables use additional message properties that get passed along with the individual messages. Then the value of any property will be kept with the message that it is attached to and not get modified by other following messages. Nodes will generally pass on any additional properties that they do not specifically use so the properties should not get lost along the way. In particular Function nodes will pass on additional properties provided that you pass on the original msg passed in.