# Why am I getting the wrong \*type\* of data in this message?

**URL:** <https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830>\
**Category:** General\
**Created:** [23 April 2023 04:58 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830 "2023-04-23T04:58:07Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 09:37 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/21 "2023-04-23T09:37:04Z")

</div>

Sorry @Colin but that doesn't help me.

_think_ and _probably_ aren't very reassuring to me.

When I look up `parseInt()` it tells me it returns an INTEGER.

> **[JavaScript Number parseInt() Method - javatpoint](https://www.javatpoint.com/javascript-number-parseint-method)**
>
> JavaScript Number parseInt() Method with example, javascript math methods, abs() method, round() method, ceil() method, floor() method, pow() method, random() method, sqrt() method, max() method, min() method, log() method, sin() method, cos()...

Where does the converting to a string come into the mechanics?

The original payload (message part, sorry.) is already a number.... Ok, not an `integer`, but a number, so the maths I do should work.

---

<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 April 2023 10:32 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/22 "2023-04-23T10:32:24Z")

</div>

> [@Trying\_to\_learn](#):
>
> Where does the converting to a string come into the mechanics

What does that link say about the type of the value that it is parsing? It says  
"The JavaScript number **parseInt()** method parses a string argument"  
so it expects a string.  
The [MDN site](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) is a better set of docs for javascript and it has this clarification in the Description:  
The `parseInt` function converts its first argument to a string, parses that string, then returns an integer or `NaN`.

> [@Trying\_to\_learn](#):
>
> maths I do should work.

Yes it does, I was just pointing out that `round()` is more efficient than `parseInt()` as it converts the number to the nearest integer without formatting it as a string and then parsing the string to turn it back to a number again.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 April 2023 10:38 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/23 "2023-04-23T10:38:54Z")

</div>

> [@Trying\_to\_learn](#):
>
> Get the original payload  
> multiply it by `10`.  
> convert it to an integer. (gets rid of all after the decimal point)  
> do all my _magic stuff_ with that number.  
> When displaying it: divide the number by `10` and `.toFixed(1)` to give it a fixed (forced) decimal accuracy.

Yes, that is exactly what you should do. Or better still, x10 and then use `parseInt(mynumber, 10)` to get the integer since that returns a number and not a string (it can be fed with a number or a string representation of a number so `parseInt("1h", 10)` will return the number 1.

* * *

Oops, sorry Colin, didn't see your answer along the same lines.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 10:40 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/24 "2023-04-23T10:40:02Z")

</div>

Ok, thanks.

So if the original value is already a number, then `parseInt` is not the right way to do it.  
But `round` is better.

Also thanks for the `MDN` site link.  
I may have to bookmark that.  
(Though as with all the other bookmarks: remembering I have it is a whole other story.)

---

<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 April 2023 10:44 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/25 "2023-04-23T10:44:26Z")

</div>

> [@Trying\_to\_learn](#):
>
> I may have to bookmark that.

MDN and W3Schools are usually the top sites when I search for a javascript function. W3Schools can be better if you are looking for simple examples, MDN is more detailed.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 April 2023 10:44 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/26 "2023-04-23T10:44:38Z")

</div>

Thanks.

I really need to learn all these tricks, but I shall say I do not want to get ahead of myself either.

I write my code the long way so I can see it better a few days/weeks later and still (I hope) understand what it does.

No offense to anyone - but optimising code to one line can be handy but also confusing if you don't really understand what is happening.

So to check:  
`parseInt("1h", 10)`  
Confusing - and I am not sure you meant it to be.  
But I have a _payload_ with `"1h"` in it from the `openweather` node.  
The line:  
`let rain_now = parseInt(msg.data.rain["1h"]*10);`

Could you explain it another way?  
Please.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [23 April 2023 10:47 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/27 "2023-04-23T10:47:43Z")

</div>

> [@Colin](#):
>
> MDN and W3Schools are usually the top sites when I search for a javascript function. W3Schools can be better if you are looking for simple examples, MDN is more detailed.

And not forgetting that ChatGPT can be really helpful when you get stuck. Though it doesn't always give you the best answer and indeed, not always the correct one, but it will give you enough clues to make progress and for fairly straight-forward things and even most really complex ones, it will give good answers.

In my browser, I have a search shortcut called `mdn` so that I can type `mdn parseInt` and get straight to the page.

---

<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:** [7 May 2023 10:48 UTC](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830/28 "2023-05-07T10:48:32Z")

</div>

Closed on 7 May 2023 10:48 UTC

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

[Previous page](https://discourse.nodered.org/t/why-am-i-getting-the-wrong-type-of-data-in-this-message/77830.md?page=1)
