# Using function() within a function node

**URL:** <https://discourse.nodered.org/t/using-function-within-a-function-node/1552>\
**Category:** General\
**Created:** [13 July 2018 10:51 UTC](https://discourse.nodered.org/t/using-function-within-a-function-node/1552 "2018-07-13T10:51:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![max95](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@max95](https://discourse.nodered.org/u/max95)\
**Post date:** [13 July 2018 10:51 UTC](https://discourse.nodered.org/t/using-function-within-a-function-node/1552/1 "2018-07-13T10:51:34Z")

</div>

I'm trying to read data from a BH1750 i2c light intensity sensor. There is no node-red node for it, so I'm using a node.js module and trying to access it from within a function node in node-red. However anything that is in a function() code seems not to be executed. I used one function node to initialise the module

var BH1750 = global.get('BH1750');  
var ls1 = new BH1750();  
flow.set('ls1', ls1)  
return msg;

and another one, from an example to read the value:

var ls1 = flow.get('ls1');  
ls1.readLight(function(err, value){  
if (err) {  
msg.payload = err;  
throw err;  
} else {  
msg.payload = value;  
}  
});

I'm not a programmer and I don't understand the nameless function() construct .... Is there a way around it?

Thanks

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [13 July 2018 10:57 UTC](https://discourse.nodered.org/t/using-function-within-a-function-node/1552/2 "2018-07-13T10:57:17Z")

</div>

Hi @Max95

for future reference, please wrap your code blocks in ``` so they format correctly.

```auto
var ls1 = flow.get('ls1');
ls1.readLight(function(err, value){
  if (err) {
    msg.payload = err;
    throw err;
  } else {
    msg.payload = value;
  }
});

```

You are providing a callback function to the `readLight` function. Within that callback, you set `msg.payload` to a value, but then do nothing else.

So as written, that callback won't do anything to send on a message or notify you in any way that it has been called.

You need to use the `node.send` function to send on the msg from within the callback, as described here - [https://nodered.org/docs/writing-functions#sending-messages-asynchronously](https://nodered.org/docs/writing-functions#sending-messages-asynchronously)

I'd also suggest you add some `node.warn("hello");` type statements within the callback so you can see what is happening - those warn messages will appear in the debug sidebar.

---

<div class="post-metadata">

**Author:** ![max95](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@max95](https://discourse.nodered.org/u/max95)\
**Post date:** [13 July 2018 11:04 UTC](https://discourse.nodered.org/t/using-function-within-a-function-node/1552/3 "2018-07-13T11:04:54Z")

</div>

Hi @knolleary.

Thank you very much for you quick and helpful reply. This has been the best forum experience of my life!

---

<div class="post-metadata">

**Author:** ![sergioaiello](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sergioaiello/32/3476_2.png) [@sergioaiello](https://discourse.nodered.org/u/sergioaiello)\
**Post date:** [28 October 2018 06:01 UTC](https://discourse.nodered.org/t/using-function-within-a-function-node/1552/4 "2018-10-28T06:01:21Z")

</div>

Hi max95, I hope you found the way to reach your goal. Would you mind to provide the solution that worked for you? Any help even little is appreciated. Thank you!

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [28 October 2018 09:37 UTC](https://discourse.nodered.org/t/using-function-within-a-function-node/1552/5 "2018-10-28T09:37:34Z")

</div>

Have a look at this post:

> [@Read BH1750 Sensor](https://discourse.nodered.org/t/read-bh1750-sensor/3378):
>
> Hi You know a way how to read the sensor data from the BH1750 sensor?
