Need help, funtion node, extracting payload

Hi
I'm New to Node-Red, but have spent several days on youtube and google trying to figure this out. I would appreciate if you could help me over this bump in the road.

What I'm trying to do, is to get information out of SignalK JSON delta messages imported through websocket, and display it on the Node-Red Dashboard.

I'm New to JavaScript, and I'm pretty sure this is where my problem is:

if (msg.updates[0].source.pgn === "129025") {
    msg.payload = msg.updates[0].values[0].value.longitude;
      return [msg, null];
}
if (msg.updates[0].source.pgn === "129025") {
    msg.payload = msg.updates[0].values[1].value.latitude;
      return [null, msg];
}

The websocket spams Node-Red With several JSON messages every second, and most of them are formatted the same way, I therefore need to check the PGN number of a Message to make sure it is the Message With the right data before extracting the sensor Readings.

For some reason my JavaScript doesn't work. Where the numbers should be on the Dashboard it's just blank. Debug gives me this:

19‎.‎02‎.‎2019‎ ‎05‎.‎03‎.‎20node: navigation.positionfunction : (error)

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

Please help :slight_smile:

I was only allowed on image per post, here is the flow if that helps:

You are comparing text to a number.

msg.updates[0].source.pgn === 129025
Also, "and most of them are formatted the same way" - you will need to capture exceptions

That says that you have tried to use [0] on an object that does not exist. That in turn means that msg.updates does not exist. So the message coming in does not have a property msg.updates, so the message coming in is not the one you show in your post.
If the reason is that some messages do not have this, and you just want to ignore these then you can replace your test with something like

if (msg.updates  &&  msg.updates[0].source.pgn === 129025) {

so it tests for msg.updates present before looking for the data.

1 Like

Also if the first
if (msg.updates[0].source.pgn === "129025") {
is true, you will never get to the second
if (msg.updates[0].source.pgn === "129025") {
since the first if does a return and the two if conditions are exactly the same.