TypeError: Cannot set property 'latitude' of undefined


I have a msg from TTN - I want to extract msg.payload_fields.latitudeDeg and msg.payload_fields.ongitudeDeg - but I get a error - "TypeError: Cannot set property 'latitude' of undefined"

My function node:


The msg




Your help will be greatly appriciated.

The current msg.payload is a string (actually is a JSON string - see the "{"batV":.. etc...} your picture) so when you attempt to set msg.payload.content you get an error - because a string is not an object.


msg.payload = {}; //set payload up as a new empty {object}
msg.payload.content = {}; //set payload.content up as a new empty {object}
msg.payload.content.whatever = whatever_i_want; //now you can add properties to the object
return msg;

few things...

  1. please always surround code or JSON with triple backticks
    like this
  2. is there any reason you set flow context then get it straight back out of context? (Also, note there is a typo in line 5 - to avoid typos, use the copy path button in the debug side-bar (it appears when you hover over a debug value)
  3. your payload already has the lat/lon but in JSON - where is this payload coming from? If it is from MQTT node, you can have the MQTT node turn the JSON into an object (set MQTT In node to auto parse JSON)

Thank you for your help, working.


Were under the impression I understood my problem and could fix, but not.

How do you add additional fields to the msg.payload?

[{"id":"10c350fc.5ced1f","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"15d52aba.2249f5","type":"function","z":"10c350fc.5ced1f","name":"","func":"msg.payload.content.latitude = flow.get(\"lat\");\nmsg.payload.content.longitude = flow.get(\"lng\");\nmsg.payload.options.live_period = flow.get(\"time\");\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":180,"wires":[["ff313d48.6f6bb"]]},{"id":"6f7c25d4.8b1a7c","type":"delay","z":"10c350fc.5ced1f","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":420,"y":180,"wires":[["15d52aba.2249f5"]]},{"id":"1878b980.91fe17","type":"inject","z":"10c350fc.5ced1f","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":240,"wires":[["bfc56da7.23c6f","6f7c25d4.8b1a7c"]]},{"id":"bfc56da7.23c6f","type":"function","z":"10c350fc.5ced1f","name":"","func":"\nflow.set(\"lat\",\"-29\");\nflow.set(\"lng\",\"30\");\nflow.set(\"time\",\"600\");\n","outputs":1,"noerr":0,"x":410,"y":240,"wires":[[]]},{"id":"ff313d48.6f6bb","type":"debug","z":"10c350fc.5ced1f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":700,"y":200,"wires":[]}]

Hello again...

As I said in previous post, you need to make payload an object.

You seem to have a fundamental lack of knowledge about node-red and javascript (no offence intended)

I understand this is just a test flow - but there is no need to be storing values in flow context in this manner - you can simply pass them in the message.

I recommend watching this playlist: Node-RED Essentials. It is by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in less than an hour. A small investment for a lot of gain.

Thank you,

I did not realize that you can not add fields if the


I have add as suggested and get


Any other methadone to change from number to object the mgs type?

Sure, you could use a template node or a change node.

When you get to grips with node-red and a little bit of javascript knowledge this would be very obvious to you.

The best thing you can do is have a play - try putting a change node before the function to set msg.payload to a JSON value (with {} as the value) - use debug nodes EVERYWHERE (after every node) - see how the msg changes as it travels through your nodes.

1 Like

When I am busy with the flow I use a lot of debug and will delete them after the flow are working correctly.

Thanks for your help.

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