Error when multiplying variables

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":

[{"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:

[{"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!

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

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

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:

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.

1 Like

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.

1 Like

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!

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:

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!

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.

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

1 Like

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!

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