# How to pass payload data from node red to mysql

**URL:** <https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875>\
**Category:** General\
**Tags:** database\
**Created:** [30 December 2021 07:45 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875 "2021-12-30T07:45:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![asyrahshah](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@asyrahshah](https://discourse.nodered.org/u/asyrahshah)\
**Post date:** [30 December 2021 07:45 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875/1 "2021-12-30T07:45:43Z")

</div>

Hello I am new with node red. Currently, I am working with node-red and mysql database for my final year project. I would like to send temperature data to the database but i have error stated that the array is undefined.

 ![node-red](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f8c80674045351da5e508aa68d6f518b56f2a145.png)  
this is my node red flow..  
and this is the function in mysql function

```auto
var test = msg.payload;
var temp = test.temperature;

msg.payload = [temp];

msg.topic = "INSERT INTO temperature(temperature) VALUES (?)";
return msg;

```

however, the `msg.payload` :

```auto
INSERT INTO temperature(temperature) VALUES (?) : msg.payload : array[1]
[undefined]

```

can you help me to figure out my problem?

---

<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:** [30 December 2021 08:30 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875/2 "2021-12-30T08:30:58Z")

</div>

Hi, welcome to the forum.

Unless you name your debug nodes it is difficult to tell from a picture.

At a guess, `msg.payload` is a number BUT here...

```auto
var test = msg.payload;
var temp = test.temperature;

msg.payload = [temp];

```

you are affectively saying "Set `msg.payload` to a new array `[]` and put `msg.payload.temperature` inside"

Try this instead...

```auto
const temp = msg.payload;
msg.payload = [temp];
msg.topic = "INSERT INTO temperature(`temperature`) VALUES (?)";
return msg;

```

* * *

PS, please remember to wrap code in three backticks  
`````  
`like this`  
`````

---

<div class="post-metadata">

**Author:** ![asyrahshah](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@asyrahshah](https://discourse.nodered.org/u/asyrahshah)\
**Post date:** [30 December 2021 12:03 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875/3 "2021-12-30T12:03:01Z")

</div>

thank you.. i got it

---

<div class="post-metadata">

**Author:** ![asyrahshah](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@asyrahshah](https://discourse.nodered.org/u/asyrahshah)\
**Post date:** [30 December 2021 12:18 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875/4 "2021-12-30T12:18:53Z")

</div>

if i want to add another column such as id from the node red together with the temperature data .. how should i write it?

 ![form](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/2730b85f156f013f9105ec1fe11b594ce7a081ac.png)

 ![id](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/63c71443a03359b8ba01161de0fada30adac3581.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:** [30 December 2021 12:39 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875/5 "2021-12-30T12:39:14Z")

</div>

if the data comes from different nodes, then you need to join the 2 bits of data together into 1 message/payload so that you can use them together in your insert query.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

**Author:** ![asyrahshah](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@asyrahshah](https://discourse.nodered.org/u/asyrahshah)\
**Post date:** [30 December 2021 13:12 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875/6 "2021-12-30T13:12:06Z")

</div>

i have tried using join, but i still got an error

 ![join](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ffcc8ff4389dc0b54c2504025e79ba7898338612.png)

 ![edit join](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/530e8e87f2f3a1ab3fe5f59a08d9ae3ea479c58c.png)

this is the debug message:

{"INSERT INTO temperature(temperature) VALUES (?)":[36.75],"INSERT INTO temperature(id) VALUES (?)":[{"id":1729336}]}

how ever, the id is not updated in mysql.  
can you please help me.

---

<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:** [30 December 2021 13:44 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875/7 "2021-12-30T13:44:01Z")

</div>

dont put the object in the payload array - only put the value.

e.g.

```auto
msg.payload = [msg.payload.id]; 

```

or if you have 2 params then update your query and add the 2 params (use debug window as instructed below)

```auto
const data = msg.payload;
msg.payload = [data.id, data.temperature];
msg.topic = "INSERT INTO temperature(`id`, `temperature`) VALUES (?,?)";
return msg;

```

↑ does that make sense?

* * *

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<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:** [13 January 2022 13:44 UTC](https://discourse.nodered.org/t/how-to-pass-payload-data-from-node-red-to-mysql/55875/8 "2022-01-13T13:44:17Z")

</div>

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