The flow gets NaN after running once or twice

Hello guys. I have a problem with saving variables recently.

I use Node-RED to build an Auto Page Sender as Telegram Bot, which automatically sends three pages of a book each day. The function node keeps track of the current page number using a flow variable. Then bot sends the page numbers via using API. After sending the pages, it increments the page number by 3, so the next day it continues from where it left off. There's no problem at all with the Telegram bot.

However, recently the flow variable started getting saved as NaN. After running once or twice, it just turns into NaN. I’ve checked my code, and I don’t see any obvious issues. I would like to understand what might be causing this problem.

Maybe it’s related to the flow context? But it worked fine for 5 months without any issues.

Could it be something on my server instead?

I’d appreciate any tips or suggestions.

How does my nodes look like?

How does my script look like?

var page_number = Number(flow.get('page_number')); //gets last saved page_number

msg.payload = {}; // creates object which is used for saving pages.
var captions = [`Today; we read between page ${page_number}-${page_number + 2}`,];

msg.payload.chat_id = -9999999; //telegram group id. (i gave a dummy value for security issues)
msg.payload.media = []; 

msg.payload.media.push({
  caption: captions[0], // Use the caption we already set
    type: "photo",
    media: `https://readbook.com/img/s${page_number}.png`
});
msg.payload.media.push({
  type:"photo",
    media: `https://readbook.com/img/s${page_number+1}.png`
})
msg.payload.media.push({
  type:"photo",
    media: `https://readbook.com/img/s${page_number+2}.png`
})
page_number+=3; //the page_number gets +3 and the updated value will be saved as flow again.
flow.set('page_number',page_number);
return msg;

Start by adding code after you fetch it to check that it is not NaN and set it to a sensible default if it is. Also use node.warn() to show a message when this happens. That will at least allow it to recover.

Are you writing (or reading) the context value anywhere else?

Don't use var, use the modern methods let or const. Google if you want to know why. It is not a factor in this problem but it is good practice.

Is it when you restart node-red that it fails? Your code will give NaN on initial startup. Change it to
let page_number = flow.get('page_number') ?? 0
That says that if the flow variable does not exist (so is undefined) then set it to 0. If 0 is not what you want to default to then use whatever is appropriate.

Does the 'readbook.com' domain actually exist?
When I visited it GoDaddy reported the domain is available for sale.

Are you writing (or reading) the context value anywhere else?

No actually i dont write or read the value anywhere else. The complete node looks like what i've snipped above.

Don't use var, use the modern methods let or const. Google if you want to know why. It is not a factor in this problem but it is good practice.

Yes i changed it to let. Thanks for that. I didnt get what should i do with node.warn() though i need to search about it.

Is it when you restart node-red that it fails? Your code will give NaN on initial startup. Change it to let page_number = flow.get('page_number') ?? 0

Actually, it’s not a problem. The value is there.

First, I set the flow value using flow.set('page_nummer', 469);. So the flow value is now 469, and I can see it.

Then I commented out the code in function with //.

So, the value is already stored in the flow. Photo below;

And now the function will be able to access the page_number value when the timestamp triggers again tomorrow.

let page_number = Number(flow.get('page_number'));.

Does the 'readbook.com' domain actually exist?

No it was also a fake dummy domain. I took the book pages from another website which is perfectly works. (As I mentioned the script runs since 5 months without having any trouble actually).

Better to use the code I posted to initialise it
let page_number = flow.get('page_number') ?? 469

If you only need it inside the node then use node context.
let page_number = context.get('page_number') ?? 469
and
context.set('page_number',page_number)

I suspect the NaN is caused by node-red restarting. Put my suggestion in and it will go back to the default if node red restarts.

By the way, you don't generally need semicolons any more.

[Edit]: Corrected above, 'I meant If you only need it inside the node then use node context.'

Put my suggestion in and it will go back to the default if node red restarts.

let page_number = flow.get('page_number') ?? 469

This is fine, but the problem is: when an error occurs while reading the value page_number from the context, it will automatically default to 469 because of the code above.

This is not ideal, because the pages between 469–472 might have already been read by the users in the group — meaning it would send outdated pages to them.

But anyway i just changed var to let and also i use context instead of flow. Maybe it'll solve the problem :slight_smile: Thanks for your help i precuate for that.

I see you are using a 'Memory only' context store.
I tend to use 'File' context for such purposes instead of 'RAM' as it's more reliable.

Imagine that if you reboot your system, (or even if it reboots itself...), you would have lost your context value, and therefore probably get a 'null'.
Whereas, saving your context to your file system would survive a reboot, and continue working.

Also, you could run a check first in your function node to ensure that the context value exists, and if it's a number, continue, and if not, log it, and stop the script.
Something like...

let value = flow.get('page_number');
// Check if the value is a number
if (typeof value === 'number' && !isNaN(value)) {
    // It's a valid number, pass it along
    const page_number = value;
} else {
    // Not a number, log a warning before stopping the script
    node.warn(`Flow stopped: context value 'page_number' is not a valid number. Value: ${value}`);
    return null; // Stop the flow
}

No, it will only go to 469 if the value is undefined, which will almost certainly only occur if node-red has restarted for some reason.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.