How to rename the message?

[{"ID":2,"EVENTTYPE":0,"MOST_RECENT_SIGN_EVENT":"2021-05-26T08:21:30.000Z"},{"ID":2,"EVENTTYPE":1,"MOST_RECENT_SIGN_EVENT":"2021-08-29T15:17:44.000Z"}]

I have an array that looks just like the above. Basically eventtypes of 0 are logouts and eventtypes of 1 are login. I'd like to be able to see of the two dates in the above, which is the most recent, because that will show me whether the user is currently still logged in.
Before I start tying myself in knots with variables and switch nodes etc I thought I'd just see if there was a logical, already accepted way of going about this!

thanks

You could use a change node with a JSONata expression, sort/order the array by the date and select the first index

$$.payload^(>MOST_RECENT_SIGN_EVENT)[0]

example flow

[{"id":"204db5e3.95ea72","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"ID\":2,\"EVENTTYPE\":0,\"MOST_RECENT_SIGN_EVENT\":\"2021-05-26T08:21:30.000Z\"},{\"ID\":2,\"EVENTTYPE\":1,\"MOST_RECENT_SIGN_EVENT\":\"2021-08-29T15:17:44.000Z\"}]","payloadType":"json","x":160,"y":2980,"wires":[["e2bac98.bc315b8","d8091ebd.a8116"]]},{"id":"e2bac98.bc315b8","type":"change","z":"c74669a0.6a34f8","name":"latest element","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload^(>MOST_RECENT_SIGN_EVENT)[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":3000,"wires":[["63461532.8efd3c"]]},{"id":"d8091ebd.a8116","type":"change","z":"c74669a0.6a34f8","name":"signed in true/false","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload^(>MOST_RECENT_SIGN_EVENT)[0].EVENTTYPE = 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":3060,"wires":[["63461532.8efd3c"]]},{"id":"63461532.8efd3c","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":3000,"wires":[]}]

[edit] Added true false resolution of if on line.

Thanks - what I'm finding is it's not liking my array, and I'm having to turn the array into a single payload like {"ID":2,"EVENTTYPE":0,"MOST_RECENT_SIGN_EVENT":"2021-05-26T08:21:30.000Z"} for it to be accepted - is this something I should fiddle around with? I can probably do this using a split node, right? But then doesn't that negate the whole point of doing a comparison? Thanks again

Sorry not understanding you. What array are you trying it with?

I'm sorry, I'm very green - is this not an array?

Assuming your array is in msg.payload, use a function node...

msg.payload = msg.payload.reduce(function (a, b) { return new Date(a.MOST_RECENT_SIGN_EVENT) > new Date(b.MOST_RECENT_SIGN_EVENT) ? a : b; });
return msg;

Sorry I think I've found out why I couldn't get the change node to accept the output from my database node. I think it's because the database node sets the name of the message to include the entire query

30/08/2021, 15:24:48[node: 9c34e936.a168f8] 
SELECT id, eventtype, MAX(datetime) AS most_recent_sign_event FROM ATTENDANCE WHERE id = 2 GROUP BY id, eventtype; : 
msg.payload : array[2]
array[2]
0: object
1: object

whereas the inject node which works looks like this:

30/08/2021, 15:24:35[node: 9c34e936.a168f8]
msg.payload : array[2]
array[2]
0: object
1: object

I can't figure out how to strip this extra detail from the name which I think is what would get the whole thing working.

thanks!

No the sql query is the msg.toipc set the debug to show complete msg, then you will see more clearly.
as below

[{"id":"c321449.38159b8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":2940,"wires":[]},{"id":"204db5e3.95ea72","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"select * from table","payload":"[{\"ID\":2,\"EVENTTYPE\":0,\"MOST_RECENT_SIGN_EVENT\":\"2021-05-26T08:21:30.000Z\"},{\"ID\":2,\"EVENTTYPE\":1,\"MOST_RECENT_SIGN_EVENT\":\"2021-08-29T15:17:44.000Z\"}]","payloadType":"json","x":160,"y":2980,"wires":[["63461532.8efd3c","c321449.38159b8"]]},{"id":"63461532.8efd3c","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":3000,"wires":[]}]

It is unclear what exact values(array/object) you are using, and how you are applying it.
Can we see a copy of the debug after the database node. Use the copy value button that appears to the right of the msg object when you hover the mouse, then paste value here using the </> option at top of the forum reply editor. Or like below.
```
code or flow JSON
```
Also a copy of the flow you are trying it on.

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