[SOLVED]How to split message& save to mysql

#1

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"

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;
0 Likes

#2

You define your query as
newMsg.topic

but you then return msg

change newMsg.topic to msg.topic

0 Likes

#3

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"
0 Likes

#4

OK. Try

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.

0 Likes

#5

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:

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;
0 Likes

#6

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

0 Likes

#7

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

0 Likes