# How to rename the message?

**URL:** <https://discourse.nodered.org/t/how-to-rename-the-message/50354>\
**Category:** General\
**Created:** [29 August 2021 17:24 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354 "2021-08-29T17:24:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [29 August 2021 17:24 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/1 "2021-08-29T17:24:10Z")

</div>

`[{"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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 August 2021 18:21 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/2 "2021-08-29T18:21:12Z")

</div>

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

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

```

example flow

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [29 August 2021 20:51 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/3 "2021-08-29T20:51:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [30 August 2021 07:37 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/4 "2021-08-30T07:37:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [30 August 2021 12:40 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/5 "2021-08-30T12:40:00Z")

</div>

> [@dclear](#):
>
> [{"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'm sorry, I'm very green - is this not an array?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 August 2021 12:58 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/6 "2021-08-30T12:58:06Z")

</div>

> [@dclear](#):
>
> 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.

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

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![dclear](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dclear/32/9107_2.png) [@dclear](https://discourse.nodered.org/u/dclear)\
**Post date:** [30 August 2021 14:38 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/7 "2021-08-30T14:38:17Z")

</div>

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

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [30 August 2021 15:02 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/8 "2021-08-30T15:02:59Z")

</div>

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

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 October 2021 15:03 UTC](https://discourse.nodered.org/t/how-to-rename-the-message/50354/9 "2021-10-29T15:03:11Z")

</div>

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