# Change javascript code in function node

**URL:** <https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891>\
**Category:** General\
**Created:** [25 September 2019 14:24 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891 "2019-09-25T14:24:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![chandra123](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@chandra123](https://discourse.nodered.org/u/chandra123)\
**Post date:** [25 September 2019 14:24 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/1 "2019-09-25T14:24:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [25 September 2019 14:31 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/2 "2019-09-25T14:31:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [25 September 2019 14:33 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/3 "2019-09-25T14:33:11Z")

</div>

Also asked in this post yesterday. [Import rules by rest api](https://discourse.nodered.org/t/import-rules-by-rest-api/15855)  
Please do not open multiple threads for the same question.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [25 September 2019 14:51 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/4 "2019-09-25T14:51:47Z")

</div>

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

Example:

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

---

<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:** [25 September 2019 17:59 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/5 "2019-09-25T17:59:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [25 September 2019 19:19 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/6 "2019-09-25T19:19:20Z")

</div>

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](https://nodered.org/docs/api/admin/methods/post/flows/). 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.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [26 September 2019 04:41 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/7 "2019-09-26T04:41:28Z")

</div>

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](https://discourse.nodered.org/t/import-rules-by-rest-api/15855)

---

<div class="post-metadata">

**Author:** ![chandra123](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@chandra123](https://discourse.nodered.org/u/chandra123)\
**Post date:** [26 September 2019 08:50 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/8 "2019-09-26T08:50:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 September 2019 08:56 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/9 "2019-09-26T08:56:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chandra123](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@chandra123](https://discourse.nodered.org/u/chandra123)\
**Post date:** [26 September 2019 09:25 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/10 "2019-09-26T09:25:20Z")

</div>

@bakman2  
Hi,  
I have written one rule as  
sensorData=JSON.parse(msg.payload);  
var a=sensordata.dp1;  
var a=sensordata.dp2;  
var c=a+b;  
msg.payload=c;  
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?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 September 2019 09:38 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/11 "2019-09-26T09:38:35Z")

</div>

As suggested;

> [@bakman2](#):
>
> use node-red-dashboard, you can create a form that an end-user can manipulate.

---

<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:** [26 September 2019 17:12 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/12 "2019-09-26T17:12:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 September 2019 17:41 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/13 "2019-09-26T17:41:00Z")

</div>

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.

> [@Import rules by rest api](https://discourse.nodered.org/t/import-rules-by-rest-api/15855):
>
> Hi, I have multiple wind turbines which are publishing data on multiple mqtt topics and using function node I am able to apply rules to filter that data and again publishing that filtered data on different mqtt topics in node red. But my concern here is,I am able to apply the rules as a developer but if an end user wants to apply or change the specific rule,how he can change that as he doesn't have knowledge of node red. Is there any way to call rest-api in node which would send the rules alo…

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 September 2019 17:41 UTC](https://discourse.nodered.org/t/change-javascript-code-in-function-node/15891/14 "2019-09-26T17:41:11Z")

</div>


