# MySQL NODE-RED table

**URL:** https://discourse.nodered.org/t/mysql-node-red-table/7612
**Category:** General
**Created:** [5 February 2019 12:11 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612 "2019-02-05T12:11:46Z")
**Posts on this page:** 14
**Page:** 2

<div class="post-metadata">

### Author: ![smalhao](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@smalhao](https://discourse.nodered.org/u/smalhao)
#### Post date: [5 February 2019 17:44 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/21 "2019-02-05T17:44:01Z")

</div>

i want sen 3 messages, the values of Inductive sensor, LM35 sensor and LM335 sensor

---

<div class="post-metadata">

### Author: ![smalhao](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@smalhao](https://discourse.nodered.org/u/smalhao)
#### Post date: [5 February 2019 17:51 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/22 "2019-02-05T17:51:29Z")

</div>

I have this in the debug

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bd5a921ea9141c3b8319ef1603f87ce1853e7024.png)  
and my function to database is  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d0006e91794685ca50464ade950a45814cc3c675.png)

Sensores is the portuguese translation of Sensors

---

<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: [5 February 2019 18:08 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/23 "2019-02-05T18:08:36Z")

</div>

Hi @smalhao

as you have changed the Join node to generate a key/value object, the `msg.payload[0]` way of accessing its properties is no longer the right way to do it.

This page in the docs explains how you can use the Debug sidebar to identify how to access different parts of a message: [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

If you look at the Debug message, you can see `msg.payload` is no longer an array, but an Object. It has three properties:

```javascript
{
   "ns=2;s=LM35": 23.0550....,
   "ns=2;s=SensorIndutivo": 7514.689.....,
   "ns=2;s=LM335": 22.2104...
}

```

So you would access those properties as:

```javascript
msg.payload["ns=2;s=LM35"]
msg.payload["ns=2;s=SensorIndutivo"]
msg.payload["ns=2;s=LM335"] 

```

in your Function node, in place of `msg.payload[0]`, `msg.payload[1]` and `msg.payload[2]`.

---

<div class="post-metadata">

### Author: ![smalhao](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@smalhao](https://discourse.nodered.org/u/smalhao)
#### Post date: [6 February 2019 10:14 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/24 "2019-02-06T10:14:38Z")

</div>

I was making a small program to test more easily and that was as follows:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7d6fce5a72c403e3d67f528e1b2b5b98fc2842c0.png)

The configuration of each is :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4e7708fb4afd0c86429141e3e640e07f53fa85b7.png)

The configuration of Joins is:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0b5cc5161c766bad05e6b60cb840cd525d8bc10f.png)

The function is 🙂  
var newmsg=new Date().toString();  
msg.topic= "INSERT INTO testebase VALUES ('"+new Date().toString()+"','"+msg.payload[0].toFixed(2)+"','"+msg.payload[1].toFixed(2)+"','"+msg.payload[2].toFixed(2)+"')";  
return msg;

The debug output of function is :  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c23fd1975bc29168149ff04dd03180f865106a2f.png)

and send the data correctly to database. When i apply the same method in other program i want (yesterday )programming don´t show the array with the values correctly.

---

<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: [6 February 2019 10:22 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/25 "2019-02-06T10:22:39Z")

</div>

Why have you changed the Join back to array mode? You need it to be the key/value object.

In array mode, it will join each 3 messages it receives into the array - regardless of the order they arrive in.

So if you have three sensors, A, B and C, and you get a stream of messages:

```auto
A B C B A B B C A

```

You will end up with `[A,B,C]`, `[B,A,B]`, `[B,C,A]`.

By using the key/value object join mode, it will use msg.topic to ensure each message has one message from each topic it receives.

---

<div class="post-metadata">

### Author: ![smalhao](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@smalhao](https://discourse.nodered.org/u/smalhao)
#### Post date: [6 February 2019 11:03 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/26 "2019-02-06T11:03:51Z")

</div>

The Join configuration I have  
is as follows:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d16869a1cd9ee0da898569b53cb729492d5d3445.png)

the output of Join is:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/3884d49e1b1fcc5fbc59c99f376d1d6cc9b527c0.png)

When i change to key/value Object

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/ab09cb49be7bcdbac4e44a5601af124b1214d9dc.png)

the output of Join is

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/64be29273bc252ea1939817ed89dbbf90e71b972.png)

and i need to send the value of sensor to database, but in database the values appears in that way

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/5d88e06430db4c3f6ac80c35874a07b5365072ea.png)  
the values 1360.30.... are meters, 20, 00 are the temperature of sensor LM335 and 20.74 are the temperature of sensor LM35 and i need to put them in column correctly.

---

<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: [6 February 2019 11:05 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/27 "2019-02-06T11:05:34Z")

</div>

What does your function node that is generating the query look like?

Please don't screenshot it. Copy the code and paste it here - making sure you've read this post [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) about how to properly share code on this forum.

---

<div class="post-metadata">

### Author: ![smalhao](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@smalhao](https://discourse.nodered.org/u/smalhao)
#### Post date: [6 February 2019 11:07 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/28 "2019-02-06T11:07:51Z")

</div>

> [@knolleary](#):
>
> What does your function node that is generating the query look like?

in the function to send data to database i configured that way:

```
var newmsg=new Date().toString();
msg.topic="INSERT INTO Sensores VALUES ('"+ new Date().toString() +"','"+ msg.payload[0] +"','"+ msg.payload[1] +"','"+ msg.payload[2] +"')";
return msg;

```

i think i missing something in the function i think

---

<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: [6 February 2019 11:09 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/29 "2019-02-06T11:09:52Z")

</div>

Did you read this reply from me? [MySQL NODE-RED table](https://discourse.nodered.org/t/mysql-node-red-table/7612/23?u=knolleary)

It explained how you needed to change your query to access the properties of msg.payload after changing the Join node configuration.

---

<div class="post-metadata">

### Author: ![smalhao](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@smalhao](https://discourse.nodered.org/u/smalhao)
#### Post date: [6 February 2019 11:12 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/30 "2019-02-06T11:12:39Z")

</div>

Put the query like this :

```
var newmsg=new Date().toString();
msg.topic="INSERT INTO Sensores VALUES ('"+ new Date().toString() +"','"+ msg.payload["ns=2;s=SensorIndutivo"] +"','"+ msg.payload["ns=2;s=LM335"] +"','"+ msg.payload["ns=2;s=LM35"] +"')";
return msg;
```

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [6 February 2019 11:20 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/31 "2019-02-06T11:20:14Z")

</div>

You need to understand the difference between an object and an array in javascript.  
It is worth spending 30minutes reading one of the many online tutorials.  
After that also read the following [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<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: [6 February 2019 11:26 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/32 "2019-02-06T11:26:44Z")

</div>

> [@smalhao](#):
>
> Put the query like this :

Okay. And did that work? What output did that generate? Did you use the Debug node to see what the query it generated?

---

<div class="post-metadata">

### Author: ![smalhao](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@smalhao](https://discourse.nodered.org/u/smalhao)
#### Post date: [6 February 2019 11:28 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/33 "2019-02-06T11:28:47Z")

</div>

Its working, thanks for your help knolleary

```
06/02/2019, 11:27:15node: output of function
INSERT INTO Sensores VALUES ('Wed Feb 06 2019 11:27:01 GMT+0000 (GMT)','1899.0927590950298','20.44695023999293','21.19148479999994') : msg.payload : Object
{ ns=2;s=LM35: 21.19148479999994, ns=2;s=LM335: 20.44695023999293, ns=2;s=SensorIndutivo: 1899.0927590950298 }

```

the database is now correctly

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a0fa7532fbf14a09b28d88fc4aaaf9963a5e81e3.png)

---

<div class="post-metadata">

### Author: ![smalhao](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@smalhao](https://discourse.nodered.org/u/smalhao)
#### Post date: [8 February 2019 12:03 UTC](https://discourse.nodered.org/t/mysql-node-red-table/7612/34 "2019-02-08T12:03:52Z")

</div>

if i need to check each one of topics if they are sending the data, how i coud make that in a function of switch case ?

[Previous page](https://discourse.nodered.org/t/mysql-node-red-table/7612.md?page=1)
