# Pass data to a function node?

**URL:** <https://discourse.nodered.org/t/pass-data-to-a-function-node/87509>\
**Category:** General\
**Created:** [26 April 2024 07:46 UTC](https://discourse.nodered.org/t/pass-data-to-a-function-node/87509 "2024-04-26T07:46:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [26 April 2024 07:46 UTC](https://discourse.nodered.org/t/pass-data-to-a-function-node/87509/1 "2024-04-26T07:46:16Z")

</div>

Hi,

I have a function node that is accessed via a call node.

The **function** sends data to a database.  
I have numerous meters that I wish to save the data into the same database.  
At the moment I copy the function and just change **device** & **Garage** and add it in another flow.

2 examples ...  
let device = "IP15";  
let location = "Garage"

let device = "IP21";  
let location = "Shed"

Is there a way to only have **one** function ... and pass the device & locations to it ?  
Thanks  
.

```auto
let meter = msg.payload
let device = "IP15"; // ENTER HERE ... the last part of the IP address.
let location = "Garage"; // ENTER HERE ... Location

let new_date = new Date();                                                           

let iso = new Date().toISOString();                                           

let timestamp = Math.round(new_date.getTime() / 1000);                                

let payload =
      { 
          iso: iso,
          location: location,      
          timestamp: timestamp,
          meter_reading: meter
      };

```

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [26 April 2024 07:58 UTC](https://discourse.nodered.org/t/pass-data-to-a-function-node/87509/2 "2024-04-26T07:58:26Z")

</div>

Pass them in on the `msg` object

e.g. `msg.location` and `msg.device`

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [26 April 2024 12:28 UTC](https://discourse.nodered.org/t/pass-data-to-a-function-node/87509/3 "2024-04-26T12:28:12Z")

</div>

> [@hardillb](#):
>
> Pass them in on the `msg` object

Thanks hardillb 🙂

That worked, so simple but took me ages to work out how to pass them on 🙄

For anyone else in the future, create a function node and then this will pass it on ...

```auto
msg.device = 'IP15';
msg.location = 'Garage';
return msg;

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 April 2024 12:38 UTC](https://discourse.nodered.org/t/pass-data-to-a-function-node/87509/4 "2024-04-26T12:38:31Z")

</div>

Also very simple and more elegant with a change node!

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [26 April 2024 12:57 UTC](https://discourse.nodered.org/t/pass-data-to-a-function-node/87509/5 "2024-04-26T12:57:59Z")

</div>

Why didn't i think of that 🙃

I usually look for existing nodes to use, never occurred to me to use that one , thanks  
So much easier and elegant, I like elegant code 👍

Thanks

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [26 April 2024 13:05 UTC](https://discourse.nodered.org/t/pass-data-to-a-function-node/87509/6 "2024-04-26T13:05:50Z")

</div>

It's very easy, especially if you are used to a more traditional programming language, to automatically reach for a function node to tackle a problem.

A perfectly valid approach but at the cost of possibly losing the editor's visual representation of the program logic

---

<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:** [25 July 2024 13:06 UTC](https://discourse.nodered.org/t/pass-data-to-a-function-node/87509/7 "2024-07-25T13:06:33Z")

</div>

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