Hello,
I have an input node that gives me a JSON with lots of keys and values. I'm able to extract the "deveui" value and the "payload" value. I'd like to publish this as a new MQTT message that is has a topic name of the value for "deveui" and a message that is the value for "payload".
I'm able to extract the two individually. But I'm unsure about how to create a new payload as input to the MQTT publish node so that the MQTT node takes dynamic topic and payloads.
Edit: Closing the loop in case someone else runs into this, I initialize the new variable to msg and the MQTT publishes work correctly now. So like this
Yes, I left the MQTT node's topic blank, so it should take on the input node's topic. Trying what you wrote, I still get the same error msg, " cannot set property 'topic'"
Do you mean you have a Function node containing only those two lines and you see that error? Are you sure it is the function node that is generating the error?
The problem with the function code you have is that you declare mynewout as a variable, but you don't initialise it to anything - it is undefined. That means you cannot set the topic and payload properties and why you get the error message you do - it literally cannot set the topic property of an undefined value.
The suggestion from @nlecaude will have the same end result - returning a message with the same payload, but the topic now set to whatever msg.deveui was. This is generally a better approach than creating a new object - it will preserve any other msg properties.
I don't think your question is related to the one in this thread, which is two years old anyway. Please start a new thread to ask your question. Note, however that you either have to specify the topic in msg.topic or in the mqtt node if it is fixed. You can't send it an array and expect it to sort it out. Send your array through a split node to split it into individual messages, (or do that earlier in the flow) and then make sure the topic is in msg.topic and the data is in msg.payload.