Function node error - "TypeError: msg.payload[0].filter is not a function"

I have flow which has been working just fine up until i updated the unifi node.
Now i get an error:
"TypeError: msg.payload[0].filter is not a function"
I'll be honest and say i no nothing about writing function nodes, so im not exactly sure how to dubug this error...but will assume the message arriving has changed in some way
Appreciate any advice

[{"id":"d11d8210d610770b","type":"inject","z":"8912d148.68c81","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":420,"wires":[["368d2906.ef3a66"]]},{"id":"368d2906.ef3a66","type":"Unifi","z":"8912d148.68c81","name":"Unifi","server":"1a296781e30e2e26","command":"20","x":310,"y":260,"wires":[["96ea60062db1758a"]]},{"id":"96ea60062db1758a","type":"function","z":"8912d148.68c81","name":"Determine device presence","func":"const lastSeenSeconds = 20;\nlet presenceCutoff = (new Date() - (lastSeenSeconds * 1000)) / 1000; \nconst people = {\n    \"Adam/presence/unifi\": \"GalaxyNote8\",\n    \"Trina/presence/unifi\": \"S22_Ultra\",\n    \"Jenny/presence/unifi\": \"Jenny-Iphone\"\n};\n\nreturn Object.keys(people).map(function(topic) {\n    let devices = msg.payload[0].filter(device => === people[topic] && device.last_seen > presenceCutoff);\n    return {\n        topic: topic,\n        retain: true,\n        payload: devices.length > 0\n    };\n});","outputs":4,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":260,"wires":[["2a4f5f6bd6cd9a46","b7f44097c3e7906a"],["b65ee81b4bd925f8"],["36af771d.ff3ce8"],[]]},{"id":"2a4f5f6bd6cd9a46","type":"rbe","z":"8912d148.68c81","name":"","func":"rbe","gap":"","start":"","inout":"out","x":730,"y":260,"wires":[["25368c2c2d704da4"]]},{"id":"b7f44097c3e7906a","type":"debug","z":"8912d148.68c81","name":"debug 12","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":400,"wires":[]},{"id":"1a296781e30e2e26","type":"unificonfig","name":"","ip":"","port":"8443","site":"default","unifios":false,"ssl":false}]

Is payload[0] an array? as filter is an array method.

I believe so
Screenshot_20220906-225431_Samsung Internet

@E1cid asked "Is payload[0] an array?"

msg.payload is an array - we can see that from screenshot however msg.payload[0] is the first element inside msg.payload & we cannot see what is inside the payload array because its not expanded in your screenshot.

Are you certain you didnt mean to do msg.payload.filter()?

I expanded the array, and it shows as an object?

Not sure tbh, ive copied this function node from elsewhere, and ony changed the client device names its searching for

then I can asure you .filter is NOT and object function

In otherwords, msg.payload IS and array and .filter is an array function.

In short, change msg.payload[0].filter( to msg.payload.filter(

1 Like

That change done the trick!
Tho now i get a red triangle above the node when i deploy, although the node/flow still works correctly

What version of node-red?

Can you share the full script?

Version 3.0.2

By script, what do you mean sorry?

Think i worked it out.
I had the number of outputs set to 4, but only 3 devices being looked for.
Changed output to 3 and error triangle now gone!

1 Like

Appreciate everyones help!

Glad I searched, also had this randomly stop working as well. I have never quite understood the second half of this code. Thanks for the fix!