Error parsing JSON to influxDB

Hi,

I have the following flow

image

v2x=JSON.parse(msg.payload);
node.log(typeof v2x);
msg.payload={
timestamp:v2x["timestamp"],
latitude: parseFloat((v2x["message"]["basic_container"]["reference_position"]["latitude"])/10000000),
longitude:parseFloat((v2x["message"]["basic_container"]["reference_position"]["longitude"])/10000000),
heading:v2x["message"]["high_frequency_container"]["heading"],
speed:v2x["message"]["high_frequency_container"]["speed"],
curvature:v2x["message"]["high_frequency_container"]["curvature"],
long_acc:v2x["message"]["high_frequency_container"]["long_acc"],
yaw_rate:v2x["message"]["high_frequency_container"]["yaw_rate"],
denm_lat:parseFloat((v2x["message"]["management_container"]["event_position"]["latitude"])/10000000),
denm_long:parseFloat((v2x["message"]["management_container"]["event_position"]["longitude"])/10000000)};
return msg;

I get the following error:

"TypeError: Cannot read property 'reference_position' of undefined"

I get the same error in "event_position"

I've installed the toFloat plugin.

Thanks

of undefined

which mean it does not exist in the incoming payload. First check the incoming messages.

If you can't work it out, add a debug node and show us the messages from both mqtt nodes.

Here the JSON files:

Those fields exist.

The second doesn’t seem to have a basic_container

Hi,

i think the problem with your flow is that you make the assumption that your two mqtt messages will arrive at the function simultaneously and that all their values will be available.
Just because they are wired to the same function doesnt mean that their values will both be accessible at that specific time. They arrive at different times so one or the other set of values will be undefined.

A way to solve this would be to use a Join node to wait for the two mqtt messages and then send the merged mqtt message to the function

Using a complete msg debug node after the Join node will make the new msg structure obvious and easier to make the necessary changes in your function.

Also if you are using Json String as an Mqtt message then you could convert it into a JS Object straight in the MQTT nodes so the will be no need to do JSON.parse() in the function

image

Hi @UnborN, I've created the following flow as you suggested:

However, I only receive information directly from MQTT, the rest of configuration is:

image

image

This inside function:

node.log(typeof v2x);
msg.payload={
timestamp:v2x["timestamp"],
latitude: parseFloat((v2x["message"]["basic_container"]["reference_position"]["latitude"])/10000000),
longitude:parseFloat((v2x["message"]["basic_container"]["reference_position"]["longitude"])/10000000),
heading:v2x["message"]["high_frequency_container"]["heading"],
speed:v2x["message"]["high_frequency_container"]["speed"],
curvature:v2x["message"]["high_frequency_container"]["curvature"],
long_acc:v2x["message"]["high_frequency_container"]["long_acc"],
yaw_rate:v2x["message"]["high_frequency_container"]["yaw_rate"],
denm_lat:parseFloat((v2x["message"]["management_container"]["event_position"]["latitude"])/10000000),
denm_long:parseFloat((v2x["message"]["management_container"]["event_position"]["longitude"])/10000000)};
return msg;

I don't get information from the other debug nodes.

Thanks

You missed an important part of the Join node configuration
image

Which tells the Join node to wait for the 2 seperate mqtt messages before joining them and sending them further down the line (to the function)

@UnborN, yes, I forgot to check that

I still have an error with the parsing

image

It says that cannot read basic_container, however it is in the left JSON structure, is it still getting the left parameters?

can you send us a new debug screenshot of the result you get after the Join node ? (debug named "1")
expand the msg to see the full structure of the message. (the important parts)

@UnborN, find it below:

basic_container lives in rx/cam/message/basic_container i am not sure that a slash is valid in a property name, but you can extract it like:

msg.payload['rx/cam'].message.basic_container

eg.

latitude: parseFloat((msg.payload['rx/cam'].message.basic_container.reference_position.latitude)/10000000)

I modified the function to:

In debug2, the output is correct, however the influxDB provokes this error:

image

I notice that long_acc is still undefined .. i dont know much about influxDb and how it handles undefined values. If you expand Debug 2 you can check if anything is missing or undefined.

Another thing you may want to test is .. i saw that during the tests and experimentations to get the right msg.payload to the influxDb out node .. you had it wired to the rest of the flow and a wrong Measurement 'table' may have been created in you db.

As a test can you create a new Measurement/table (to start clean) .. or Drop the existing one
IF no important data are in the db !!!

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