# Error when multiplying variables

**URL:** <https://discourse.nodered.org/t/error-when-multiplying-variables/45459>\
**Category:** General\
**Created:** [10 May 2021 21:43 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459 "2021-05-10T21:43:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![omalisson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/omalisson/32/32797_2.png) [@omalisson](https://discourse.nodered.org/u/omalisson)\
**Post date:** [10 May 2021 21:43 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/1 "2021-05-10T21:43:05Z")

</div>

Good night friends!

I'm having trouble making a multiplication between two variables where the result output is "NaN".  
When I do the multiplication using the operand as the input payload and multiply it by a global variable, it works.

Below is my flow with the result "NaN":

```auto
[{"id":"dae1fa58.ce9168","type":"ui_template","z":"6e917fb5.ac771","group":"6f658e33.ccd82","name":"Total R$","order":1,"width":0,"height":0,"format":"<div ng-bind-html=\"msg.payload\"></div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":700,"y":200,"wires":[["a24447fb.42a658"]]},{"id":"1b51bd6c.6b1273","type":"function","z":"6e917fb5.ac771","name":"","func":"var conv = msg.payload[0];\nvar dolar = flow.get(\"usdt\");\nvar total = conv*dolar;\n\nmsg.payload = 'R$ '+total;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":200,"wires":[["dae1fa58.ce9168"]]},{"id":"22a1b4a.ffdfa4c","type":"http request","z":"6e917fb5.ac771","name":"USDT","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://www.infomoney.com.br/cotacoes/tether-usdt/","tls":"","persist":false,"proxy":"","authType":"","x":270,"y":200,"wires":[["adffe6f0.7f85f8"]]},{"id":"d7c3558c.b91758","type":"inject","z":"6e917fb5.ac771","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"300","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":200,"wires":[["22a1b4a.ffdfa4c"]]},{"id":"adffe6f0.7f85f8","type":"html","z":"6e917fb5.ac771","name":"total","property":"payload","outproperty":"payload","tag":"div.value>p","ret":"html","as":"single","x":420,"y":200,"wires":[["95966ffb.5c222","a24447fb.42a658","1b51bd6c.6b1273"]]},{"id":"a24447fb.42a658","type":"debug","z":"6e917fb5.ac771","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":160,"wires":[]},{"id":"6f658e33.ccd82","type":"ui_group","z":"","name":"Saldo em Reais","tab":"5fb77a02.18daa4","order":3,"disp":true,"width":"6","collapse":false},{"id":"5fb77a02.18daa4","type":"ui_tab","z":"","name":"Demo - CCM","icon":"visibility","disabled":false,"hidden":false}]

```

Here is the test done using the node input:

```auto
[{"id":"c47b3966.e36868","type":"ui_template","z":"6e917fb5.ac771","group":"6f658e33.ccd82","name":"Total R$","order":1,"width":0,"height":0,"format":"<div ng-bind-html=\"msg.payload\"></div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":680,"y":340,"wires":[["5a192a65.78aca4"]]},{"id":"5a192a65.78aca4","type":"debug","z":"6e917fb5.ac771","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":320,"wires":[]},{"id":"de12327.f91cdd","type":"function","z":"6e917fb5.ac771","name":"","func":"var conv = msg.payload;\nvar dolar = flow.get(\"usdt\");\nvar total = conv*dolar;\n\nmsg.payload = 'R$ '+total;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":340,"wires":[["c47b3966.e36868"]]},{"id":"f247e14c.1c70a","type":"inject","z":"6e917fb5.ac771","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"300","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"str","x":310,"y":340,"wires":[["de12327.f91cdd"]]},{"id":"6f658e33.ccd82","type":"ui_group","z":"","name":"Saldo em Reais","tab":"5fb77a02.18daa4","order":3,"disp":true,"width":"6","collapse":false},{"id":"5fb77a02.18daa4","type":"ui_tab","z":"","name":"Demo - CCM","icon":"visibility","disabled":false,"hidden":false}]

```

I thank you for your attention and help from the Friends!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 May 2021 22:11 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/2 "2021-05-10T22:11:23Z")

</div>

Try outputing your vars to see what you have. I think one will be undefined.

```auto
var conv = msg.payload[0];
var dolar = flow.get("usdt");
var total = conv*dolar;
node.warn(`dolar is - ${dolar}, conv is - ${conv}`)
msg.payload = 'R$ '+total;
return msg;

```

---

<div class="post-metadata">

**Author:** ![omalisson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/omalisson/32/32797_2.png) [@omalisson](https://discourse.nodered.org/u/omalisson)\
**Post date:** [10 May 2021 23:09 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/3 "2021-05-10T23:09:44Z")

</div>

Thanks for the return Friend!

How do I know if the variable has been defined as undefined when in the debug node it has the desired output?  
See the print:

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 May 2021 07:19 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/4 "2021-05-11T07:19:01Z")

</div>

Have you tried the code example "node.warn()" to output your vars? This will allow you to see what your vars when the code runs.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [12 May 2021 12:05 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/5 "2021-05-12T12:05:08Z")

</div>

> [@E1cid](#):
>
> `var conv = msg.payload[0];`

Are you expecting the input payload to be an array? From the screen shot, it appears to be a string, but i'm not sure -- if the payload is a string, then you will be getting only the first character from it, and I doubt that is what you want...

As E1cid pointed out, add more debug nodes to your flow, and warn statements to your function code.

---

<div class="post-metadata">

**Author:** ![omalisson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/omalisson/32/32797_2.png) [@omalisson](https://discourse.nodered.org/u/omalisson)\
**Post date:** [12 May 2021 13:12 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/6 "2021-05-12T13:12:21Z")

</div>

Good morning mate!  
In fact I ended up not testing your version, because I confess that I understood that your intention was to know if the variables "dollar" and "conv" were receiving values (which is true) or if they were empty.  
I will add your line and perform the test to see the result.

Thanks for the feedback!

---

<div class="post-metadata">

**Author:** ![omalisson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/omalisson/32/32797_2.png) [@omalisson](https://discourse.nodered.org/u/omalisson)\
**Post date:** [12 May 2021 13:21 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/7 "2021-05-12T13:21:59Z")

</div>

Good morning mate!  
Yes, the payload is an array, hence the indication of the position "[0]" which is where the information I am looking for is located.  
See the print below:

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

I will add the code of our friend E1cid to help debug this case and return with the information.

Thank you very much for your support!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 May 2021 13:31 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/8 "2021-05-12T13:31:02Z")

</div>

Your issue is msg.payload[0] is "R$ 5,4".  
When you times this string by any number you will get a NaN, You need to remove the "R$ " from the string. Also replace the "," with a "."  
e.g.

```auto
var conv = Number(msg.payload[0].substr(2).replace(",", "."));

```

---

<div class="post-metadata">

**Author:** ![omalisson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/omalisson/32/32797_2.png) [@omalisson](https://discourse.nodered.org/u/omalisson)\
**Post date:** [12 May 2021 15:30 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/9 "2021-05-12T15:30:38Z")

</div>

Truth!  
As the decimal separators are different between BRL and USD in fact the account does not close.  
As for the prefix "R $" I already remove it exactly with the "substr" when I create the variable.  
I'll do this test this afternoon to see if it works!

Thank you one more time!

---

<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:** [11 July 2021 15:31 UTC](https://discourse.nodered.org/t/error-when-multiplying-variables/45459/10 "2021-07-11T15:31:22Z")

</div>

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