# Problem with Jsonata function $toMillis

**URL:** <https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453>\
**Category:** General\
**Created:** [11 March 2021 08:14 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453 "2021-03-11T08:14:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xenokrates](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xenokrates/32/35880_2.png) [@Xenokrates](https://discourse.nodered.org/u/Xenokrates)\
**Post date:** [11 March 2021 08:14 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/1 "2021-03-11T08:14:13Z")

</div>

I see a strange error thrown by the JSONATA $toMillis() function, when I try to process a payload from the feedparser node.

- The 'article.date' field from the feedparser node seems to yield a perfect DateTime string.
- When I send it directly to a CHANGE node with "Set msg.payload to [Jsonata]$toMillis(payload) I get an error:

> Invalid JSONata expression: Argument 1 of function "toMillis" does not match function signature

-When I take the payload string from the DEBUG node and INJECT it to the same CHANGE node, I get the correct result.

I'm clueless. Any suggestions ?!

Here the example flow:

```auto
[{"id":"4f58bfb4.3dbd3","type":"inject","z":"66117b0a.627124","name":"Timestamp","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2021-03-11T07:04:00.000Z","payloadType":"str","x":230,"y":940,"wires":[["abb60e71.a108e8"]]},{"id":"19791ded.25089a","type":"debug","z":"66117b0a.627124","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":670,"y":760,"wires":[]},{"id":"763dd87d.e23a68","type":"feedparse","z":"66117b0a.627124","name":"Heise","url":"https://www.heise.de/rss/heise-atom.xml","interval":"120","x":70,"y":840,"wires":[["17b52f3.021a951"]]},{"id":"17b52f3.021a951","type":"trigger","z":"66117b0a.627124","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"0","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":250,"y":840,"wires":[["19791ded.25089a","f82ddcce.bd3278"]]},{"id":"f82ddcce.bd3278","type":"change","z":"66117b0a.627124","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"article.date","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":820,"wires":[["617be7c.0f74c98","abb60e71.a108e8"]]},{"id":"617be7c.0f74c98","type":"debug","z":"66117b0a.627124","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":670,"y":820,"wires":[]},{"id":"abb60e71.a108e8","type":"change","z":"66117b0a.627124","name":"Jsonata $toMillis","rules":[{"t":"set","p":"payload","pt":"msg","to":"$toMillis(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":880,"wires":[["ad804cbb.85b5a"]]},{"id":"ad804cbb.85b5a","type":"debug","z":"66117b0a.627124","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":670,"y":880,"wires":[]}]

```

---

<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:** [11 March 2021 08:20 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/2 "2021-03-11T08:20:45Z")

</div>

`article.date` as set in first change node seems wrong. can we see a debug of msg going into this change node?

---

<div class="post-metadata">

**Author:** ![Xenokrates](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xenokrates/32/35880_2.png) [@Xenokrates](https://discourse.nodered.org/u/Xenokrates)\
**Post date:** [11 March 2021 08:52 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/3 "2021-03-11T08:52:34Z")

</div>

Thanks, this is msg object going into change node `set msg.payload`:

```auto
{"topic":"https://www.heise.de/news/Visual-Studio-Code-kommt-fuer-Apple-Silicon-5076987.html?wt_mc=rss.red.ho.ho.atom.beitrag.beitrag","payload":"<p><a href=\"https://www.heise.de/news/Visual-Studio-Code-kommt-fuer-Apple-Silicon-5076987.html?wt_mc=rss.red.ho.ho.atom.beitrag.beitrag\"><img src=\"https://www.heise.de/scale/geometry/450/q80//imgs/18/3/0/6/9/8/4/3/vsc-6e0729f85d8457df.png\" class=\"webfeedsFeaturedVisual\" alt=\"\" /></a></p><p>Microsoft hat seinen quelloffenen Developer-Editor nun für ARM-Macs angepasst. Das Update verspricht Leistungsgewinne.</p>","article":{"title":"Visual Studio Code kommt für Apples ARM-Macs","description":"<p><a href=\"https://www.heise.de/news/Visual-Studio-Code-kommt-fuer-Apple-Silicon-5076987.html?wt_mc=rss.red.ho.ho.atom.beitrag.beitrag\"><img src=\"https://www.heise.de/scale/geometry/450/q80//imgs/18/3/0/6/9/8/4/3/vsc-6e0729f85d8457df.png\" class=\"webfeedsFeaturedVisual\" alt=\"\" /></a></p><p>Microsoft hat seinen quelloffenen Developer-Editor nun für ARM-Macs angepasst. Das Update verspricht Leistungsgewinne.</p>","summary":"Microsoft hat seinen quelloffenen Developer-Editor nun für ARM-Macs angepasst. Das Update verspricht Leistungsgewinne.","date":"2021-03-11T08:30:00.000Z","pubdate":"2021-03-11T08:30:00.000Z","pubDate":"2021-03-11T08:30:00.000Z","link":"https://www.heise.de/news/Visual-Studio-Code-kommt-fuer-Apple-Silicon-5076987.html?wt_mc=rss.red.ho.ho.atom.beitrag.beitrag","guid":"http://heise.de/-5076987","author":"heise online","comments":null,"origlink":null,"image":{},"source":{},"categories":[],"enclosures":[],"atom:@":{},"atom:title":{"@":{"type":"text"},"#":"Visual Studio Code kommt für Apples ARM-Macs"},"atom:id":{"@":{},"#":"http://heise.de/-5076987"},"atom:updated":{"@":{},"#":"2021-03-11T09:30:00+01:00"},"atom:published":{"@":{},"#":"2021-03-11T09:30:00+01:00"},"atom:link":{"@":{"href":"https://www.heise.de/news/Visual-Studio-Code-kommt-fuer-Apple-Silicon-5076987.html?wt_mc=rss.red.ho.ho.atom.beitrag.beitrag"}},"atom:summary":{"@":{"type":"html"},"#":"Microsoft hat seinen quelloffenen Developer-Editor nun für ARM-Macs angepasst. Das Update verspricht Leistungsgewinne."},"atom:content":{"@":{"type":"html"},"#":"<p><a href=\"https://www.heise.de/news/Visual-Studio-Code-kommt-fuer-Apple-Silicon-5076987.html?wt_mc=rss.red.ho.ho.atom.beitrag.beitrag\"><img src=\"https://www.heise.de/scale/geometry/450/q80//imgs/18/3/0/6/9/8/4/3/vsc-6e0729f85d8457df.png\" class=\"webfeedsFeaturedVisual\" alt=\"\" /></a></p><p>Microsoft hat seinen quelloffenen Developer-Editor nun für ARM-Macs angepasst. Das Update verspricht Leistungsgewinne.</p>"},"meta":{"#ns":[{"xmlns":"http://www.w3.org/2005/Atom"}],"@":[{"xmlns":"http://www.w3.org/2005/Atom"}],"#xml":{"version":"1.0","encoding":"utf-8"},"#type":"atom","#version":"1.0","title":"heise online News","description":"Nachrichten nicht nur aus der Welt der Computer","date":"2021-03-11T08:30:00.000Z","pubdate":"2021-03-11T08:30:00.000Z","pubDate":"2021-03-11T08:30:00.000Z","link":"https://www.heise.de/","xmlurl":"https://www.heise.de/rss/heise-atom.xml","xmlUrl":"https://www.heise.de/rss/heise-atom.xml","author":"heise online","language":null,"favicon":null,"copyright":"Copyright (c) 2021 Heise Medien","generator":null,"cloud":{},"image":{},"categories":[],"atom:@":{"xmlns":"http://www.w3.org/2005/Atom"},"atom:title":{"@":{"type":"text"},"#":"heise online News"},"atom:subtitle":{"@":{"type":"html"},"#":"Nachrichten nicht nur aus der Welt der Computer"},"atom:updated":{"@":{},"#":"2021-03-11T09:30:00+01:00"},"atom:id":{"@":{},"#":"https://www.heise.de/rss/heise-atom.xml"},"atom:author":{"@":{},"name":{"@":{},"#":"heise online"},"uri":{"@":{},"#":"https://www.heise.de"}},"atom:link":[{"@":{"rel":"self","type":"application/atom+xml","href":"https://www.heise.de/rss/heise-atom.xml"}},{"@":{"rel":"alternate","type":"text/html","href":"https://www.heise.de/"}}],"atom:rights":{"@":{},"#":"Copyright (c) 2021 Heise Medien"}}},"_msgid":"8bcc8b27.364478"}

```

---

<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:** [11 March 2021 09:16 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/4 "2021-03-11T09:16:07Z")

</div>

Ok with that data everything works as it should this end. So i am failing to understand what is going wrong for you.  
As seen in this image.

 ![Screenshot_20210311-091347](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fba71003228246c19c2863144a5e8d1c2d2e69dc.png)  
The function just creates the payload and article objects, pasted from your debug.

---

<div class="post-metadata">

**Author:** ![Xenokrates](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xenokrates/32/35880_2.png) [@Xenokrates](https://discourse.nodered.org/u/Xenokrates)\
**Post date:** [11 March 2021 09:51 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/5 "2021-03-11T09:51:23Z")

</div>

Huuuuh, finally found it:

The feedparser node sends a JavaScript [Date-Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) in the attribute `article.date` !  
(See [line 28](https://github.com/danmactough/node-feedparser/blob/master/lib/feedparser/index.js) in the source of the underlying JavaScript feedparser node at GitHub.)

This is totally obscured by the Node\_RED GUI (!) which shows a string as debug output.  
But naturally JSONATA fails, because it only handles strings.

To solve this I now change the attribute in a function node:

> msg.payload = msg.payload.toISOString();  
> return msg;

Here the whole example flow:

```auto
[{"id":"17b52f3.021a951","type":"trigger","z":"66117b0a.627124","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"0","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":300,"y":820,"wires":[["f82ddcce.bd3278"]]},{"id":"f82ddcce.bd3278","type":"change","z":"66117b0a.627124","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"article.date","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":820,"wires":[["617be7c.0f74c98","ac9811bf.c7e59","fba4f7ca.92d86"]]},{"id":"617be7c.0f74c98","type":"debug","z":"66117b0a.627124","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":730,"y":820,"wires":[]},{"id":"abb60e71.a108e8","type":"change","z":"66117b0a.627124","name":"Jsonata $toMillis","rules":[{"t":"set","p":"payload","pt":"msg","to":"$toMillis(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":880,"wires":[["ad804cbb.85b5a"]]},{"id":"ad804cbb.85b5a","type":"debug","z":"66117b0a.627124","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":770,"y":880,"wires":[]},{"id":"441e3174.9974e8","type":"feedparse","z":"66117b0a.627124","name":"NYT","url":"https://rss.nytimes.com/services/xml/rss/nyt/Technology.xml","interval":"120","x":90,"y":820,"wires":[["17b52f3.021a951"]]},{"id":"ac9811bf.c7e59","type":"function","z":"66117b0a.627124","name":"","func":"msg.payload = msg.payload.toISOString();\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":880,"wires":[["abb60e71.a108e8"]]},{"id":"fba4f7ca.92d86","type":"change","z":"66117b0a.627124","name":"Jsonata $toMillis","rules":[{"t":"set","p":"payload","pt":"msg","to":"$toMillis(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":940,"wires":[["63867716.5cf438"]]},{"id":"63867716.5cf438","type":"debug","z":"66117b0a.627124","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":770,"y":940,"wires":[]}]

```

@dceejay - It's a bad thing, that it is not visible in the GUI, that some attributes of feedparser node output are not, what the debug node shows. I am not sure, if this should be _sanitized_ by a type cast to the ISO date string that is already automagically shown in debug node or how this _tripping hazard_ may be cleared.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 March 2021 10:22 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/6 "2021-03-11T10:22:26Z")

</div>

> [@Xenokrates](#):
>
> To solve this I now change the attribute in a function node:
> 
> > msg.payload = msg.payload.toISOString();  
> > return msg;

It does not seem right to convert a Date object (which internally is actually the milliseconds value) to a string then use JSONata to convert it back to milliseconds. Alternatively, in the function node you could convert it directly to milliseconds.  
`msg.payload = msg.payload.getTime()`

---

<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:** [11 March 2021 10:34 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/7 "2021-03-11T10:34:06Z")

</div>

That makes sense. The same thing in jsonata would be to convert to string and remove \" from the string

```auto
$toMillis($replace($string(payload),/\"/, ""))

```

---

<div class="post-metadata">

**Author:** ![Xenokrates](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xenokrates/32/35880_2.png) [@Xenokrates](https://discourse.nodered.org/u/Xenokrates)\
**Post date:** [11 March 2021 10:52 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/8 "2021-03-11T10:52:10Z")

</div>

Yes, you're right. It seems not good to convert the attributes there and back.  
But it is a bad thing that the real object type is invisble in the GUI editor.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [11 March 2021 11:08 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/9 "2021-03-11T11:08:11Z")

</div>

If you set the debug to show msg.article.date it does show you the correct type  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/7809122be59856906797b8dccca0d8d0cf8fedd4.png)

there is no standard way to depict different types in a log as far as I know - so we chose to approximately follow the chrome browser javascript log convention.

---

<div class="post-metadata">

**Author:** ![Xenokrates](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xenokrates/32/35880_2.png) [@Xenokrates](https://discourse.nodered.org/u/Xenokrates)\
**Post date:** [11 March 2021 11:37 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/10 "2021-03-11T11:37:39Z")

</div>

Ok, you are right. I have to learn more about node-red debugging 🤔

---

<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:** [10 May 2021 11:38 UTC](https://discourse.nodered.org/t/problem-with-jsonata-function-tomillis/42453/11 "2021-05-10T11:38:33Z")

</div>

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