Dear,
I would like to request a new feature in the Change-Node, to allow Mustache expressions to be used.
It can be implemented using a single line of code (see below), similar to the HttpRequest node.
Problem description
Suppose there is a global variable 'topics' containing following data:
A message arrives with following two fields:
I want to use a single Change-node to apply the new value to topic2 in the global json array.
If I'm not mistaken this is currently not possible, but correct me if I'm wrong !!!!!!!!
Possible solution
I have added following code to the 15-change.js file:
The result
When I use now following Mustache formatted expression:
In following flow:
[{"id":"cce10850.07aec8","type":"inject","z":"d0d4f76e.209b68","name":"Inject new value","topic":"TOPIC2","payload":"new value","payloadType":"str","repeat":"","crontab":"","once":false,"x":803.6668033599854,"y":805.6666679382324,"wires":[["5a39220.3d2f7e"]]},{"id":"5a39220.3d2f7e","type":"change","z":"d0d4f76e.209b68","name":"Set last value","rules":[{"t":"set","p":"topics['{{topic}}'].last_value","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":997.6668643951416,"y":805.6666679382324,"wires":[[]]},{"id":"f78c767c.de6eb8","type":"inject","z":"d0d4f76e.209b68","name":"Initialize global context","topic":"MAGN_KEUK","payload":"{\"TOPIC1\":{\"last_value\":\"old value\"},\"TOPIC2\":{\"last_value\":\"old value\"},\"TOPIC3\":{\"last_value\":\"old value\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"x":784.6666259765625,"y":760.6666259765625,"wires":[["9ce689a6.09fe38"]]},{"id":"9ce689a6.09fe38","type":"change","z":"d0d4f76e.209b68","name":"Set global variable","rules":[{"t":"set","p":"topics","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1009,"y":760.6666259765625,"wires":[[]]}]The result seems to be correct:
Open issues
I opened an issue for this feature, but Nick already found a major drawback of this solution:
Introducing mustache support is something we've chosen not to do in general - but I can see the use case you're raising isn't easily handled otherwise (short of using a Function node). A significant issue will be doing this in a way that doesn't break existing flows - however unlikely, "{{payload}}" is a valid identifier that an existing flow may be using. If we start parsing it as mustache it will break.
If somebody is aware of other drawbacks, alternatives, solutions, ... I would be glad to hear your opinions about this!!
Perhaps an extra checkbox (to enable Mustache content), but that might be too complex for non-technical users?
Thanks in advance !!!
Bart Butenaers
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/d7455243-81fa-47bd-a453-4374ce7d8735%40googlegroups.com.
myGlobalVar["{{payload.property1}}"]["{{payload.property2}}"]I think you have a good use case here for us to look at; determining the property you want to set dynamically is only possible with the Function node today.Within the context of a Set property action, a JSONata expression is not a good fit - it gets evaluated and returns an value. That value could be any type... an object, an array, a string, a number... how that would then map to either a msg, flow or global property isn't obvious.I think the mustache route is probably the right way to go - the only question is how it is exposed. I had my original reservation that introducing it could break a flow without an option to enable it. I wonder what the true risk of that is; adding a checkbox to enable it is ugly. Or maybe there's another way to tackle - open to suggestions.Nick
On 9 August 2017 at 12:58, Julian Knight <j.kni...@gmail.com> wrote:
Personally, I think that the idea of JSONata in the what to update field seems a bit heavy but I do like the idea of being able to use data from the msg and data from an environment value as standard in such cases, the use case you give is one that I hit fairly regularly and have to use a function node to deliver.In terms of JSONata, the lack of the ability to update a specific object property is really frustrating as it blocks the use of JSONata in a number of otherwise useful cases.
On Tuesday, 8 August 2017 22:51:15 UTC+1, Bart Butenaers wrote:Hi folks,I thought that mustache expressions would be a nice (and powerful) addition to the Change node.However the number of replies on this thread was not overwhelming ...
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
--
http://nodered.org
** We're moving this mailing list over to the new Node-RED Forum: https://discourse.nodered.org **
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/b6af8439-6704-4c1e-938e-e588a6b1e81c%40googlegroups.com.