# Function node red error

**URL:** <https://discourse.nodered.org/t/function-node-red-error/18431>\
**Category:** General\
**Created:** [26 November 2019 06:38 UTC](https://discourse.nodered.org/t/function-node-red-error/18431 "2019-11-26T06:38:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samidsamid](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@samidsamid](https://discourse.nodered.org/u/samidsamid)\
**Post date:** [26 November 2019 06:38 UTC](https://discourse.nodered.org/t/function-node-red-error/18431/1 "2019-11-26T06:38:22Z")

</div>

why does the local function turn on when node red can't be injected ??

```auto
(function(window) {
  var methods = {},
    init;

    methods.ambil = function(simpenstr, opsi) {
    var ambil = {
      Type: undefined,
      Length: undefined,
      Value: undefined,
      Parsing: undefined
    };
  
    
    ambil.Type = simpenstr.substring(0, opsi.panjangambil);
    ambil.Type = String(parseInt(ambil.Type, 16).toString(2));
    kurang = String(parseInt(ambil.Type, 16).toString(2));
    aku = parseInt(ambil.Type, 16).toString(2);
    ambil.Type = ambil.Type.slice(0, -(opsi.panjangambil));
    simpenstr = simpenstr.substring(opsi.panjangambil);
    
    if (ambil.Length = kurang.toString().slice(-2) == "00"){
      ambil.Length = "01"
    }else if (ambil.Length = kurang.toString().slice(-2) == "0"){
      ambil.Length = "01"
    }else if (ambil.Length = kurang.toString().slice(-2) == "11"){
      ambil.Length = "110"
    }else if (ambil.Length = kurang.toString().slice(-2) == "01"){
      ambil.Length = "10"
    }else if (ambil.Length = kurang.toString().slice(-2) == "1"){
      ambil.Length = "10"
    }else {
      ambil.Length = "100"
    };
    

    desimal = (parseInt(ambil.Length, 2).toString(16).toUpperCase
    ());

    ambil.Value = String(simpenstr).substr(0, desimal * 2).toString();
    
    ambil.Parsing = parseInt(ambil.Value, 16);

    simpenstr = simpenstr.substring(desimal * 2);
    return {
      tag: ambil,
      retStr: simpenstr
    };
  };

  methods.parserdata = function(tlv, keluaranArray, opsi) {
    while (tlv) {
      var res = methods.ambil(tlv, opsi);
      keluaranArray.push(res.tag);
      tlv = res.retStr;
    }
    return keluaranArray;
  };

  init = methods;

  var parser = init;
  

})("undefined" !== typeof window ? window : {});

var tlv = '08000ce440242a00012116450e1012000186a0' ;

var opsi = {
panjangambil: 2
};

var keluaran = [];
// console.log(parser.parserdata(tlv, keluaran, opsi));
var oke = global.set('parser');
tee = (parser.parserdata(tlv, keluaran, opsi)).slice(-1);
msg.payload = tee;
return msg;

```

---

<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:** [26 November 2019 07:57 UTC](https://discourse.nodered.org/t/function-node-red-error/18431/2 "2019-11-26T07:57:12Z")

</div>

Small suggestion - as it is going to be said at some time.

Start again and when you are about to enter the code from the function node, prefix it with 3 ` symbols.  
That is the key usually just to the left of the 1 key.  
Paste the code, press and put another 3 of that symbol.

That will format the code correctly so people can read it better.

---

<div class="post-metadata">

**Author:** ![samidsamid](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@samidsamid](https://discourse.nodered.org/u/samidsamid)\
**Post date:** [26 November 2019 08:07 UTC](https://discourse.nodered.org/t/function-node-red-error/18431/4 "2019-11-26T08:07:32Z")

</div>

thanks for the advice.  
I have confirmed my code

---

<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:** [26 November 2019 08:41 UTC](https://discourse.nodered.org/t/function-node-red-error/18431/5 "2019-11-26T08:41:32Z")

</div>

> [@samidsamid](#):
>
> why does the local function turn on when node red can't be injected

I am sorry that question does not make sense.  
If your function node is not returning the correct data then you can use`node.warn()` to display values from the function in the debug window so you can work out what is not performing as you expect. See [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions) for how to do this and for other useful information about writing functions.
