Noob JSON issues

The following JSON is passed to Node-RED from a traccar server:

{"event":{"id":683,"attributes":{},"deviceId":1,"type":"deviceUnknown","serverTime":"2021-01-26T03:31:58.515+0000","positionId":0,"geofenceId":0,"maintenanceId":0},"device":{"id":1,"attributes":{},"groupId":0,"name":"Lorens Phone","uniqueId":"XXXXXX","status":"unknown","lastUpdate":"2021-01-26T03:21:58.505+0000","positionId":91959,"geofenceIds":[1,2],"phone":"","model":"","contact":"","category":null,"disabled":false},"users":[{"id":2,"attributes":{"speedUnit":"mph","distanceUnit":"mi","timezone":"America/Chicago","notificationTokens":"tokenstuffhere"},"name":"Loren","login":"","email":"myemail","phone":"","readonly":false,"administrator":false,"map":"custom","latitude":36.339871,"longitude":-86.557218,"zoom":18,"twelveHourFormat":false,"coordinateFormat":"","disabled":false,"expirationTime":null,"deviceLimit":-1,"userLimit":0,"deviceReadonly":false,"token":null,"limitCommands":false,"poiLayer":"","password":null}]}

I would like to send the information via telegram to my phone for debugging. I'm getting an error:

TypeError: Cannot read property 'type' of undefined

Here is the function that is causing the error:

var incomming = msg.payload;

if(incomming.event["type"] && incomming["geofence"]){
    if(incomming.event.type === "geofenceEnter" || "geofenceExit"){
        var name = incomming.device.name
        var fence = incomming.geofence.name
        if(incomming.event.type === "geofenceEnter"){
            var direction = "Entered"
            msg.payload ={
                chatId:'XXXXXX',
                type: 'message',
                content: name + " " + direction + " " + fence
            }
        }
        else if(incomming.event.type === "geofenceExit"){
            var direction = "exited"
            msg.payload ={
                chatId:'XXXXXX',
                type: 'message',
                content: name + " " + direction + " " + fence    
            }
        }
        else if(incomming.event.type === "deviceOnline"){
            msg.payload ={
                chatId:'XXXXXX',
                type: 'message',
                content: name + " " + incomming.event.type
            }
        }else{
            msg.payload ={
                chatId:'XXXXXX',
                type: 'message',
                content: incomming
            }
        }
        
    }
    else{
        msg.payload ={
            chatId:'XXXXXX',
            type: 'message',
            content: incomming
        }
    }
    
    
}
else{
msg.payload ={
    chatId:'XXXXXX',
    type: 'message',
    content: incomming
    }
}
return msg;

What am I doing wrong?

Put a debug node BEFORE this function, is there a msg.payload.event.type in the debug message?


This will not work ( || "geofenceExit" will always evaluate to true)...

do this instead...
if(incomming.event.type === "geofenceEnter" || incomming.event.type === "geofenceExit")


try this...

var incomming = msg.payload;

//check parts are present in payload - warn if not.
// NOTE: you might want to add or remove checks as suited by your application
// I would probably check that `incomming` is an object and that `incomming.event` is an object
if(!incomming || !incomming.event || !incomming.geofence) {
    node.warn(["invalid payload", incomming]);
    return;
}

//default message to send
msg.payload = {
            chatId:'XXXXXX',
            type: 'message',
            content: JSON.stringify(incomming)
        }
        
if(incomming.event.type && incomming.geofence){
    if(incomming.event.type === "geofenceEnter" || incomming.event.type === "geofenceExit"){
        let name = incomming.device.name
        let fence = incomming.geofence.name
        if (incomming.event.type === "geofenceEnter") {
            msg.payload.content = name + " Entered " + fence //update content
        } else if (incomming.event.type === "geofenceExit"){
            msg.payload.content = name + " exited " + fence //update content
        } else if (incomming.event.type === "deviceOnline"){
            msg.payload.content = name + " " + incomming.event.type
        } else {
            msg.payload.content = name + " " + JSON.stringify(incomming) //update content
        }
    } 
} 

return msg;

@Steve-Mcl thanks so much. This looks really good. I've implemented it and see how today goes and let you know what I find.

This is working really well thanks for your help!

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