I am trying to get the payload data in a template node inside a JS tag.
The template node is not the one that is part of the dashboard library.
Outside the JS code, you can get the payload by using mustache tags {{.}}.
Any idea how can I get the data in JS code?
These are the nodes:
This is the code for it:
[{"id":"11a1f4fa.478f8b","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"df800fc8.3dc23","type":"http in","z":"11a1f4fa.478f8b","name":"","url":"/test","method":"get","upload":false,"swaggerDoc":"","x":280,"y":200,"wires":[["4e46196.87169e8"]]},{"id":"4e46196.87169e8","type":"function","z":"11a1f4fa.478f8b","name":"dummy payload","func":"msg.payload = {n 'data1': 15,n 'data2': 20n};nreturn msg;","outputs":1,"noerr":0,"x":500,"y":200,"wires":[["b0ce68d7.a40808"]]},{"id":"b0ce68d7.a40808","type":"template","z":"11a1f4fa.478f8b","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This is the payload: {{payload.data1}} !n<script>n(function(scope){n scope.$watch('msg', function(msg){n console.log(msg); n });n})(scope);n</script>","output":"str","x":670,"y":200,"wires":[["a111562a.d4f948"]]},{"id":"a111562a.d4f948","type":"http response","z":"11a1f4fa.478f8b","name":"","statusCode":"200","headers":{},"x":900,"y":200,"wires":[]}]
Advertisement
Answer
The template
node you are using in that flow is not the node provided by Node-RED Dashboard.
Node-RED Dashboard provides the ui_template
node that is in the Dashboard category of the palette. However you would not use the ui_template
node in the middle of an HTTP In
/HTTP Response
flow that you have here. The ui_template
node can only be used as part of a Node-RED Dashboard.
You are using the template
node that is provided by the core of Node-RED. This node is used to generate static text from a template. It uses the mustache syntax to allow you to insert values from the message passed to the node and then returns the text generated from the template.
So if you want to insert values from the msg
passed to the template node into the <script>
section of the template, then you still use the same mustache syntax. Given the template:
This is the payload: {{payload.data1}} ! <script> console.log("{{payload.data1}}"); console.log("{{payload.data2}}"); </script>
If you pass in the example message from your flow:
msg.payload = { 'data1': 15, 'data2': 20 }
Then the template node will return:
This is the payload: 15 ! <script> console.log("15"); console.log("20"); </script>