Msg.payload : undefined message

Thanks in advance,
Here is a snippet of a flow I am working with (newbie to node-red)
image

here is the function code I am using

var newMsg = {
    "topic": "INSERT INTO SENSORDATA VALUES ( " + msg.payload.time + ", " + msg.payload.id + ", " + msg.payload.type + ", " + msg.payload.data + ")"
}
return newMsg;

Here are the debug messages

3/5/2023, 10:42:13 PMnode: debug 5
fdrs/data : msg.payload : Object
{ id: 1, type: 1, data: 22.06900024, time: 49534 }
3/5/2023, 10:42:13 PMnode: debug 7
INSERT INTO SENSORDATA VALUES ( 49534, 1, 3, 53) : msg.payload : undefined
undefined

My question is why do I have the msg.payload undefined error? The data seems to have been transferred correctly, so I am not sure what the problem is?

You create a new msg with newMsg. You add topic to it and nothing else. Then you return it and there is no payload, because you never added it.
Try adding it, e.g.

var newMsg = {
    "topic": "INSERT INTO SENSORDATA VALUES ( " + msg.payload.time + ", " + msg.payload.id + ", " + msg.payload.type + ", " + msg.payload.data + ")"
}
newMsg.payload = msg.payload;

return newMsg;

Or using OG msg

msg.topic = {
    "topic": "INSERT INTO SENSORDATA VALUES ( " + msg.payload.time + ", " + msg.payload.id + ", " + msg.payload.type + ", " + msg.payload.data + ")"
}
return msg;
1 Like

Thanks, sorry I was under the mistaken impression that the returned msg always became the payload msg.

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