Change javascript code in function node

Can we change Javascript code or rule written in function by external code or rule?
by rest-api or something else

You mean by having the code evaluated based on what comes in through the payload? Should be possible but is a huge security risk.

Also asked in this post yesterday. Import rules by rest api
Please do not open multiple threads for the same question.


Use an http endpoint, listen for url parameters and use that value in the function node (if this is the same question as @ukmoose refers to).


[{"id":"36b2aa68.f0c22e","type":"template","z":"908a6b8f.bc81b","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <head></head>\n    <body>\n        \n        <pre>Value set to: {{req.query.value}}</pre>\n    </body>\n</html>","output":"str","x":482,"y":216,"wires":[["c71905a9.2c4a6","55263430.81fe6c"]]},{"id":"6d837eb1.40c64","type":"http in","z":"908a6b8f.bc81b","name":"","url":"/test","method":"get","upload":false,"swaggerDoc":"","x":348,"y":216,"wires":[["36b2aa68.f0c22e"]]},{"id":"a471e444.76cbd8","type":"debug","z":"908a6b8f.bc81b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":240,"wires":[]},{"id":"c71905a9.2c4a6","type":"http response","z":"908a6b8f.bc81b","name":"","statusCode":"","headers":{},"x":626,"y":192,"wires":[]},{"id":"6cff4544.0fa4b4","type":"comment","z":"908a6b8f.bc81b","name":"Open url: http://<ip>:1880/test?value=12","info":"","x":448,"y":144,"wires":[]},{"id":"55263430.81fe6c","type":"function","z":"908a6b8f.bc81b","name":"","func":"if(msg.hasOwnProperty(\"req\")){\n    i = parseInt(msg.req.query.value)\n}\nelse{\n    // input from different node or set it to a default value\n    i = msg.payload\n}\n\n\nreturn {payload:i}\n","outputs":1,"noerr":0,"x":626,"y":240,"wires":[["a471e444.76cbd8"]]},{"id":"7d660147.652b88","type":"inject","z":"908a6b8f.bc81b","name":"","topic":"","payload":"30","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":482,"y":288,"wires":[["55263430.81fe6c"]]}]

Or use node-red-dashboard, you can create a form that an end-user can manipulate.

Isn't there an admin API that lets you replace flows or an I confused?

Yes -- that would be a "brute-force" approach... http post the new user parameters to a node-red flow that gets the currently running flow, uses some code or expression to modify the affected nodes' attributes/configuration values, and POST the whole flow as json back to the running server using the admin /flows api. A self-modifying flow, if you will... gutsy, but technically possible. I give it a month before it self destructs!

A better alternative may be to http post the new user parameters to a flow that puts them into the appropriate flow context variables, which are then used by the data aggregation logic from that point forward. It may take a while to work through your UI charts, but at least there is no deployment step required.


Could we please keep this discussion in one thread and close the other? There have already been some ideas here: Import rules by rest api


Hi @bakman2 I appreciate your effort but you are changing the payload.
I want to change the rule not payload

Well to be honest, it is not simple to interpret what you are actually looking for as you didn't give us much to work on.

I have written one rule as
var a=sensordata.dp1;
var a=sensordata.dp2;
var c=a+b;
return msg;

this is addition rule of 2 datapoints
Now I want to change it to multiplication rule by external rest-api or anything possible.
how can I do that?

As suggested;

Can you explain why you want to do that? What is happening to the answer from this function?

I'm locking this thread as the OP already had an active thread on this topic and we can't keep replying to the same thing in two places.

1 Like