# \[SOLVED\]How to split message& save to mysql

**URL:** https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401
**Category:** General
**Created:** [24 September 2018 03:38 UTC](https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401 "2018-09-24T03:38:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![junoprima](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/junoprima/32/63547_2.png) [@junoprima](https://discourse.nodered.org/u/junoprima)
#### Post date: [24 September 2018 03:38 UTC](https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401/1 "2018-09-24T03:38:02Z")

</div>

Hello i'm new in node-red and mysql i need to put data from udp to mysql  
if i send direct without split message i can insert it into my sql

i got this error

```
"msg.topic : the query is not defined as a string"

```

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

This my function code to put in mysql with split message

```
    var newMsg = msg.payload.split(',');
    var water = { payload: newMsg[0]};
    var rssi = { payload: newMsg[1]};
    newMsg.topic = "INSERT INTO `uhm_electromagnetic`.`water_no1` (`water_status`,`rssi`) VALUES ("+msg.payload[0]+","+ msg.payload[1] +")";
    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: [24 September 2018 06:13 UTC](https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401/2 "2018-09-24T06:13:45Z")

</div>

You define your query as  
newMsg.topic

but you then return msg

change newMsg.topic to msg.topic

---

<div class="post-metadata">

### Author: ![junoprima](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/junoprima/32/63547_2.png) [@junoprima](https://discourse.nodered.org/u/junoprima)
#### Post date: [24 September 2018 08:49 UTC](https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401/3 "2018-09-24T08:49:11Z")

</div>

i think it's my wrong copy code real it's like this

```
var newMsg = msg.payload.split(',');
water = { payload: newMsg[0]};
rssi = { payload: newMsg[1]};
newMsg.topic = "INSERT INTO `uhm_electromagnetic`.`water_no1` (`water_status`,`rssi`) VALUES ("+newMsg[0]+","+ newMsg[1] +")";
return newMsg;

```

and get this error

```
"Function tried to send a message of type string"
```

---

<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: [24 September 2018 09:01 UTC](https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401/4 "2018-09-24T09:01:32Z")

</div>

OK. Try

```auto
var newMsg = msg.payload.split(',');
water = { payload: newMsg[0]};
rssi = { payload: newMsg[1]};
msg.topic = "INSERT INTO `uhm_electromagnetic`.`water_no1` (`water_status`,`rssi`) VALUES ("+newMsg[0]+","+ newMsg[1] +")";
return msg;

```

You would need to make newMsg an object but personally I find it easier to read if you keep newMsg as the array and just return the original msg object you started with.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 September 2018 09:21 UTC](https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401/5 "2018-09-24T09:21:13Z")

</div>

> [@junoprima](#):
>
> var newMsg = msg.payload.split(',');

I think this line is the culprit. You are creating a variable called newMsg but creating it as a string, then trying to send it on and it is not an object.

If you are going to reuse the original msg object (a good practice) change that to something less confusing - like myDataArray - so you have:

```auto
var myDataArray = msg.payload.split(',');
var water = myDataArray[0];
var rssi = myDataArray[1];
msg.topic = "INSERT INTO `uhm_electromagnetic`.`water_no1` (`water_status`,`rssi`) VALUES ("+water+","+ rssi +")";
return msg;

```

---

<div class="post-metadata">

### Author: ![junoprima](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/junoprima/32/63547_2.png) [@junoprima](https://discourse.nodered.org/u/junoprima)
#### Post date: [24 September 2018 09:21 UTC](https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401/6 "2018-09-24T09:21:34Z")

</div>

Wow it's work thank so much i'm forget a little bit

---

<div class="post-metadata">

### Author: ![junoprima](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/junoprima/32/63547_2.png) [@junoprima](https://discourse.nodered.org/u/junoprima)
#### Post date: [24 September 2018 09:23 UTC](https://discourse.nodered.org/t/solved-how-to-split-message-save-to-mysql/3401/7 "2018-09-24T09:23:26Z")

</div>

I try this it's work too thank you so much
