Good day everyone,
I come up with a hobby project which for sure is keeping me active on this forum.
Searching for, and learning a lot from, other forum entries.
I have searched the forum for topics that might tell me how to handle undefined in an array.
But i haven't come accross one that pointed me in the right direction, but that could be the result of the way I search as well.
This sort of comes from this topic: How to store & update NMEA-AIS data?
I am getting an array from which some parts are 'undefined'.
I have a function node that prepares it for MYSQL. But the node of MYSQL doesn't write anything to the database.
This is my function node:
msg.topic = "INSERT or REPLACE INTO AIS VALUES ( " + msg.payload[0].mmsi + "," + msg.payload[0].channel + ", " + msg.payload[0].messageType + ","+ msg.payload[0].repeatIndicator +"," + msg.payload[0].navigationStatus+","+msg.payload[0].sog+","+ msg.payload[0].positionAccurate+","+msg.payload[0].lon+", "+msg.payload[0].lat+","+msg.payload[0].timeStamp+","+msg.payload[0].raim+","+msg.payload[0].radioStatus+","+msg.payload[0].talkerId+","+msg.payload[0].sentenceId+","+msg.payload[0].talkerId_text+","+msg.payload[0].sentenceId_text+","+msg.payload[0].messageType_text+","+msg.payload[0].navigationStatus_text+","+msg.payload[0].bearing+","+msg.payload[0].distanceKM+","+ msg.payload[0].distanceNM+","+msg.payload[0].version+","+msg.payload[0].shipId+","+msg.payload[0].callsign+","+msg.payload[0].name+","+msg.payload[0].shipType+","+msg.payload[0].dimensionToBow+","+msg.payload[0].dimensionToStern+","+msg.payload[0].dimensionToPort+","+msg.payload[0].dimensionToStarboard+","+msg.payload[0].fixType+","+msg.payload[0].eta+","+msg.payload[0].draught+","+msg.payload[0].destination+","+msg.payload[0].dte+","+msg.payload[0].shipType_text+","+msg.payload[0].fixType_text+","+msg.payload[0].turningDirection+","+msg.payload[0].cog+","+msg.payload[0].trueHeading+","+msg.payload[0].turningDirection_text+","+msg.payload[0].originalMessage+")"
return msg;
But what leaves the function node is a lot of 'undefined'.
INSERT or REPLACE INTO AIS VALUES ( 013646568,A, 1,undefined,undefined,0,true,5.520333333333333, 53.18111833333333,undefined,true,0100000100001110001,AI,VDM,Mobile AIS station,AIS VHF data-link message,Position Report Class A,undefined,251.73879507739878,2.9603108494349866,1.5984399732661974,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,0,undefined,undefined,!AIVDM,1,1,,A,10=0rr?P00PIAB@NKS?h0?wP2PQi,0*6C)
MYSQL doesn't show any errors:
I assume this is because of the undefined in the array.
How can I remove the undefined?
The undefined parts might come later in another message, but that message will have some undefined as well.
Do I need to make different arrays (one for each type of AIS message, or can I keep it in one and filter out the undefined?
this array goes into the function node:
[{"channel":"A","messageType":1,"mmsi":"013646568","sog":0,"positionAccurate":true,"lon":5.520333333333333,"lat":53.18111833333333,"raim":true,"radioStatus":"0100000100001110001","talkerId":"AI","sentenceId":"VDM","talkerId_text":"Mobile AIS station","sentenceId_text":"AIS VHF data-link message","messageType_text":"Position Report Class A","bearing":251.73879507739878,"distanceKM":2.9603108494349866,"distanceNM":1.5984399732661974,"cog":0,"originalMessage":"!AIVDM,1,1,,A,10=0rr?P00PIAB@NKS?h0?wP2PQi,0*6C"}]