Hi,
I am tring to improve my JSONata skills (Yup, I'm a SQL guy loving JSONata aggregate functions...) So I downloaded the flows from https://flows.nodered.org/flow/29fd01f8a62fec86d875ecbd68001cb0 and tried them out. All the exmples return arrays of empty objects. I suspect the evaluate method of JSONata is asynchronous and that is the problem here. But promises and callbacks and awaits and node.send hurt my brain and I give up. More coffee is not helping.
Can anybody please show me how to get this simple example working?
Here is a simple string length example from the picture above
[{"id":"97198c59935dad84","type":"function","z":"2b674150e0c6799d","g":"ef756230b8be8f60","name":"$length(str)","func":"// $length(string)\n\nconst example = [];\nexample[0] = \"hello world\";\nexample[1] = [\"test array\"];\nexample[2] = false;\nexample[3] = null;\n\n// Returns the number of characters in the string\nconst expression = [];\nexpression[0] = jsonata(`$length(\"${example[0]}\")`);\nexpression[1] = jsonata(`$length(\"${example[1]}\")`);\nexpression[2] = jsonata(`$length(\"${example[2]}\")`);\nexpression[3] = jsonata(`$length(\"${example[3]}\")`);\n\nmsg.payload = [];\nmsg.payload[0] = expression[0].evaluate();\nmsg.payload[1] = expression[1].evaluate();\nmsg.payload[2] = expression[2].evaluate();\nmsg.payload[3] = expression[3].evaluate();\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"jsonata","module":"jsonata"}],"x":290,"y":120,"wires":[["139d3d075aef541b"]]},{"id":"c4e236b5f403ca46","type":"inject","z":"2b674150e0c6799d","g":"ef756230b8be8f60","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":110,"y":120,"wires":[["97198c59935dad84"]]},{"id":"139d3d075aef541b","type":"debug","z":"2b674150e0c6799d","g":"ef756230b8be8f60","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":120,"wires":[]}]
Cheers, Chris