# Error in Function Node

**URL:** https://discourse.nodered.org/t/error-in-function-node/34080
**Category:** General
**Created:** [11 October 2020 04:57 UTC](https://discourse.nodered.org/t/error-in-function-node/34080 "2020-10-11T04:57:09Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![bachloo.rahul](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bachloo.rahul](https://discourse.nodered.org/u/bachloo.rahul)
#### Post date: [11 October 2020 04:57 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/1 "2020-10-11T04:57:09Z")

</div>

![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/11a9f0d9186a47f0eda95fd2675c745d88034cc1.png)  
the 2nd function node is having code for decoder function.and msg.payload contains a buffer containing array of values.  
 ![16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/552fa50bedc330b485631067bf8d0e8e32714d65.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 October 2020 07:49 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/2 "2020-10-11T07:49:43Z")

</div>

Select the function node & export the flow JSON for that 1 node so we can have a look.

I suspect you pasted some code from a web page & managed to pick up a non printable character.

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [11 October 2020 07:55 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/3 "2020-10-11T07:55:13Z")

</div>

I see you are using JSON.parse and it fails. Try to debug the input to this node to be sure you are feeding valid JSON string to that function.

---

<div class="post-metadata">

### Author: ![bachloo.rahul](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bachloo.rahul](https://discourse.nodered.org/u/bachloo.rahul)
#### Post date: [11 October 2020 07:59 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/4 "2020-10-11T07:59:40Z")

</div>

[flows.json](https://discourse.nodered.org/uploads/short-url/uRwpLKSmKU4rmmt0vjZBsTbsd4a.json) (4.4 KB)  
Here is the file for all nodes  
2)This file is for just 1 node  
[flows (1).json](https://discourse.nodered.org/uploads/short-url/xOsYTujyWB497Zs5BiboldjvbZi.json) (538 Bytes)

This is code for decoding hexadecimal input which is the same as used in thingsnetwork for decoding the payload.I am sending data of Join packet, it is not regular report packets, so the converter can not decode it easily so i have used base64 node to convert it in buffer.

 ![14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/a/ea15f866ab45f502b368affece8bbbfe7838dbcc.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 October 2020 08:28 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/5 "2020-10-11T08:28:27Z")

</div>

msg.payload is already a javascript object, so when you try to parse it it throughs an error.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [11 October 2020 08:47 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/6 "2020-10-11T08:47:58Z")

</div>

Why are you trying to JSON.parse the incoming binary message ? I gave you the function yesterday - and you seem to have broken it... revert it to what I gave you.

> [@Converting Base 64 to Hexadecimal](https://discourse.nodered.org/t/converting-base-64-to-hexadecimal/34031/16):
>
> Here are the docs you need to help here is the first part var bytes = msg.payload; if (bytes[0]==01) { var temperature = (bytes[3] \<\< 8) + bytes[2]; var humidity= bytes[6]; return { payload: { temperature: temperature/10, humidity: humidity/2 } } } // else { // var power=bytes[2]; // return { // power: power // } // } return msg;

---

<div class="post-metadata">

### Author: ![bachloo.rahul](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bachloo.rahul](https://discourse.nodered.org/u/bachloo.rahul)
#### Post date: [11 October 2020 08:59 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/7 "2020-10-11T08:59:29Z")

</div>

this code didn't worked i tried it many times.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [11 October 2020 09:05 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/8 "2020-10-11T09:05:47Z")

</div>

Why have you added that first function node - the base64 node expects a string... not json (that your function node is trying to create). you should just need mqtt -\> base64 -\> function -\> debug

---

<div class="post-metadata">

### Author: ![bachloo.rahul](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bachloo.rahul](https://discourse.nodered.org/u/bachloo.rahul)
#### Post date: [11 October 2020 09:24 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/9 "2020-10-11T09:24:09Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/25eb03f15f432ffb787531bc2ed66c966736aeb5.png)  
This is code for 1st function node.I will try without it and debug the output.  
Also Here is exported flows[flows.json](https://discourse.nodered.org/uploads/short-url/uRwpLKSmKU4rmmt0vjZBsTbsd4a.json) (4.4 KB)

---

<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: [10 December 2020 09:24 UTC](https://discourse.nodered.org/t/error-in-function-node/34080/10 "2020-12-10T09:24:09Z")

</div>

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