# The flow gets NaN after running once or twice

**URL:** <https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268>\
**Category:** General\
**Created:** [23 July 2025 12:11 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268 "2025-07-23T12:11:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yusufscelik](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yusufscelik/32/98689_2.png) [@yusufscelik](https://discourse.nodered.org/u/yusufscelik)\
**Post date:** [23 July 2025 12:11 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/1 "2025-07-23T12:11:59Z")

</div>

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?

 ![problem](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/bae54e1b8e428c1c338373670813472cd72f4d95.png)

#### How does my script look like?

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 July 2025 12:35 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/2 "2025-07-23T12:35:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 July 2025 12:40 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/3 "2025-07-23T12:40:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [23 July 2025 12:42 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/4 "2025-07-23T12:42:18Z")

</div>

Does the '[readbook.com](http://readbook.com)' domain actually exist?  
When I visited it GoDaddy reported the domain is available for sale.

---

<div class="post-metadata">

**Author:** ![yusufscelik](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yusufscelik/32/98689_2.png) [@yusufscelik](https://discourse.nodered.org/u/yusufscelik)\
**Post date:** [23 July 2025 14:45 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/5 "2025-07-23T14:45:26Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![yusufscelik](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yusufscelik/32/98689_2.png) [@yusufscelik](https://discourse.nodered.org/u/yusufscelik)\
**Post date:** [23 July 2025 14:50 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/6 "2025-07-23T14:50:51Z")

</div>

> 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;

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff94c8a85ecba9f4bd40d0e0d26d09efb2ca0d37.png)

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'));`.

---

<div class="post-metadata">

**Author:** ![yusufscelik](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yusufscelik/32/98689_2.png) [@yusufscelik](https://discourse.nodered.org/u/yusufscelik)\
**Post date:** [23 July 2025 14:53 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/7 "2025-07-23T14:53:32Z")

</div>

> Does the '[readbook.com](http://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).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 July 2025 15:00 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/8 "2025-07-23T15:00:56Z")

</div>

> [@yusufscelik](#):
>
> First, I set the flow value using` flow.set('page_nummer', 469);`. So the flow value is now 469, and I can see it.

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.'

---

<div class="post-metadata">

**Author:** ![yusufscelik](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/yusufscelik/32/98689_2.png) [@yusufscelik](https://discourse.nodered.org/u/yusufscelik)\
**Post date:** [23 July 2025 15:13 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/9 "2025-07-23T15:13:13Z")

</div>

> 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 🙂 Thanks for your help i precuate for that.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [23 July 2025 15:39 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/10 "2025-07-23T15:39:22Z")

</div>

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...

```auto
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
}

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 July 2025 16:06 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/11 "2025-07-23T16:06:48Z")

</div>

> [@yusufscelik](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 August 2025 16:07 UTC](https://discourse.nodered.org/t/the-flow-gets-nan-after-running-once-or-twice/98268/12 "2025-08-06T16:07:30Z")

</div>

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