Problem with Jsonata function $toMillis

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:

[{"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":[]}]

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

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

{"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"}

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.


The function just creates the payload and article objects, pasted from your debug.

Huuuuh, finally found it:

The feedparser node sends a JavaScript Date-Object in the attribute article.date !
(See line 28 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:

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

1 Like

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()

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

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

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.

If you set the debug to show msg.article.date it does show you the correct type
image

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.

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

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