节点红色:全局变量有问题

时间:2019-10-11 08:32:51

标签: node-red

我必须使用全局或流持久变量,并且在某些用途上遇到麻烦:在简单函数中使用它可以,我有一个很好的vakue(使用.set或.get)的返回消息,但是当我尝试在脚本中获取变量以将其发送到html页面时,它不起作用! (使用相同的语法)

一个主意?我的节点是红色的

[{"id":"1aed4db1.318612","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"8165070c.f8bec8","type":"template","z":"1aed4db1.318612","name":"html","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE html>\n<html>\n <head>\n  <meta charset=\"utf-8\"/>\n  \n  \n </head>\n <body onload=\"draw();\">\n   <canvas id=\"canvas\" width=\"500\" height=\"500\"></canvas>\n    <script>{{{payload.draw}}}</script>\n </body>\n</html>\n\n\n\n","output":"str","x":410,"y":200,"wires":[["e46f2202.f0adb"]]},{"id":"6460da00.6d5c68","type":"http in","z":"1aed4db1.318612","name":"","url":"/test","method":"get","upload":false,"swaggerDoc":"","x":60,"y":200,"wires":[["6d9dba5b.d5a514"]]},{"id":"e46f2202.f0adb","type":"http response","z":"1aed4db1.318612","name":"","statusCode":"","headers":{},"x":630,"y":200,"wires":[]},{"id":"94994060.2fe75","type":"debug","z":"1aed4db1.318612","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":500,"y":140,"wires":[]},{"id":"9a0d36a3.66cc08","type":"function","z":"1aed4db1.318612","name":"","func":"total=flow.get(\"test\");\nmsg.payload=total;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":140,"wires":[["94994060.2fe75"]]},{"id":"d76ddfb4.1bee6","type":"inject","z":"1aed4db1.318612","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":140,"wires":[["9a0d36a3.66cc08"]]},{"id":"946553b2.76d2f","type":"config","z":"1aed4db1.318612","name":"","properties":[{"p":"test","pt":"flow","to":"\"essai\"","tot":"str"}],"active":true,"x":90,"y":80,"wires":[]},{"id":"6d9dba5b.d5a514","type":"template","z":"1aed4db1.318612","name":"Draw","field":"payload.draw","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"    function draw() {\n        var canvas = document.getElementById(\"canvas\");\n        var ctx2 = canvas.getContext(\"2d\");\n        ctx2.fillStyle = 'red';\n        ctx2.fillRect(10, 10, 50, 50);\n        ctx2.fillText(flow.get(\"test\"),200,20);\n     }\n","output":"str","x":210,"y":200,"wires":[["8165070c.f8bec8"]]}]

2 个答案:

答案 0 :(得分:0)

您似乎想在模板节点的正文中使用flow.get()。由于该代码正在无法访问Node-RED中实际运行的任何内容的远程代理上执行,因此将无法正常工作。

如果要包括流/上下文变量,则需要将它们添加到传递到模板节点的msg对象中,然后使用小胡子语法将其替换为模板。

答案 1 :(得分:0)

好吧,我做到了,而且效果不错,但是...

我添加了“ essai = {{payload}};”在我的模板中,我从消息中获得了价值,但不是全部!例如,如果我发送了一个类似[100,200,300,400]的数组,那么在调试输出中就可以了:味精正在发送相同的内容,但是在我的模板中,我只有第一个元素:“ 100” ...我试图转换将其转换为字符串,但结果相同。

如何从“ essai = {{payload}};”中获取整个数组或值? ? 我在模板节点中的代码:

  function draw() {

var essai=[];
            essai={{payload}};
            var canvas = document.getElementById("canvas");
            var ctx = canvas.getContext("2d");
             ctx.fillStyle = 'red';
            ctx.fillRect(10, 10, 50, 50);
            ctx.fillText(essai, 100,100);
    }

还有用于发送味精的功能节点:

var total=[];
total=flow.get("agvs_montage");
msg.payload=total;

return msg;

发送的值为[100,200,300,400],并且填充文本仅显示“ 100” ...