节点红色“ TypeError:无法读取未定义的属性'有效负载'”

时间:2018-06-29 13:00:50

标签: javascript jquery html node-red node-redis

我正在尝试创建一个连接到数据库的仪表板。 一切正常,但是当我尝试使用SQL查询来过滤结果时出现错误:

  

“ TypeError:无法读取未定义的属性'payload'”

如果我将“日期”部分切除,则输出仍然有效。

Debug_Window Picture of my nodes

  1. 日期部分(选择2个日期并尝试显示它们之间的信息)
  2. SQL查询+连接+输出到表
  3. 按Machine_ID过滤(MAT4和MAT6是Machine_Names)


节点红色


功能节点抛出错误:

[{"id":"a87a533e.7502c","type":"function","z":"5c6013ad.23ba3c","name":"Query_ID","func":"var machine_id = msg.machineId.payload;\nvar beginDate = msg.begindate.payload;\nvar endDate = msg.enddate.payload;\n\n\n\nmsg.topic = \"SELECT processdata.time,eqpm_machines.name AS machine_name,tags.Id AS tags_no,tags.name,processdata.value,tags.units,processdata.item_serial_no FROM processdata JOIN tags ON processdata.tag_id=tags.Id JOIN eqpm_machines ON processdata.machine_id=eqpm_machines.Id WHERE 1\";\n\nif(machine_id !== 0){\n    msg.topic = msg.topic + \" AND processdata.machine_id = \" + machine_id ;\n}\n\n\nif(beginDate !== \"\" && endDate !== \"\"){\n    msg.topic = msg.topic + \" AND processdata.time >= '\" + beginDate + \"' AND processdata.time <= '\" + endDate + \"'\" ;\n}\n\n\nif(1 == 1){\n    msg.topic = msg.topic + \";\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":140,"wires":[["527131e3.e2df","95da523b.7b8f"]]}]

所有节点:

[{"id":"70cb9a7a.7d32f4","type":"subflow","name":"Dropdown_for_MachineID","info":"","in":[{"x":200,"y":180,"wires":[{"id":"5698dce4.98ed94"}]}],"out":[{"x":900,"y":320,"wires":[{"id":"ab37a9a2.6d2648","port":0}]}]},{"id":"785c286a.4073e8","type":"mysql","z":"70cb9a7a.7d32f4","mydb":"6f90c35b.00d35c","name":"","x":530,"y":200,"wires":[["4b472402.9312dc","525cddf2.159384"]]},{"id":"3ad59ab4.36a996","type":"inject","z":"70cb9a7a.7d32f4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":280,"wires":[["5698dce4.98ed94"]]},{"id":"5698dce4.98ed94","type":"function","z":"70cb9a7a.7d32f4","name":"Query","func":"var id = msg.payload.id;\nvar machine_id = msg.payload.machine_id;\nvar tag_id = msg.payload.tag_id;\n\nmsg.topic = \"SELECT processdata.Id,processdata.time,eqpm_machines.Id AS machine_id ,eqpm_machines.name AS machine_name,tags.Id AS tags_no,tags.name,processdata.value,tags.units,processdata.item_serial_no FROM processdata JOIN tags ON processdata.tag_id=tags.Id JOIN eqpm_machines ON processdata.machine_id=eqpm_machines.Id GROUP BY machine_id HAVING 1\";\n\n\n/*\nif(machine_id !== \"\"){\n    msg.topic = msg.topic + \" AND processdata.machine_id =\" + machine_id ;\n}\n\nif(tag_id !== \"\"){\n    msg.topic = msg.topic + \" AND processdata.tag_id=  \" + tag_id ;\n}\n\n\nif(1 == 1){\n //   msg.topic = msg.topic + \"AND INNER JOIN \";\n    msg.topic = msg.topic + \";\";\n}\n*/\n\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":200,"wires":[["785c286a.4073e8"]]},{"id":"ab37a9a2.6d2648","type":"function","z":"70cb9a7a.7d32f4","name":"","func":"msg.options = msg.payload;\nreturn msg;\n\n","outputs":1,"noerr":0,"x":710,"y":320,"wires":[[]]},{"id":"525cddf2.159384","type":"change","z":"70cb9a7a.7d32f4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.{machine_name:machine_id}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":420,"wires":[["9e25a7e5.868ff8","ab37a9a2.6d2648"]]},{"id":"9e25a7e5.868ff8","type":"debug","z":"70cb9a7a.7d32f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":810,"y":420,"wires":[]},{"id":"4b472402.9312dc","type":"debug","z":"70cb9a7a.7d32f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":160,"wires":[]},{"id":"de4f6679.aaa758","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"77006751.545558","type":"mysql","z":"de4f6679.aaa758","mydb":"6f90c35b.00d35c","name":"Datenbank","x":1050,"y":380,"wires":[["6f674497.02751c","d2ca02c6.d422e","e222ef82.8faae"]]},{"id":"2f8e5fc5.9e2de","type":"function","z":"de4f6679.aaa758","name":"Query_ID","func":"var machine_id = msg.machineId.payload;\nvar beginn_date = msg.payload.begindate.payload;\nvar end_date = msg.payload.enddate.payload;\n\nmsg.topic = \"SELECT processdata.time,eqpm_machines.name AS machine_name,tags.Id AS tags_no,tags.name,processdata.value,tags.units,processdata.item_serial_no FROM processdata JOIN tags ON processdata.tag_id=tags.Id JOIN eqpm_machines ON processdata.machine_id=eqpm_machines.Id WHERE 1\";\n\nif(machine_id !== \"\" && beginn_date !== \"\" && end_date !==\"\"){\n    msg.topic = msg.topic + \" AND processdata.machine_id =\" + machine_id + \" AND processdata.time >= \" + beginn_date + \" AND processdata.time <= \" + end_date;\n}\n\nif(1 == 1){\n //   msg.topic = msg.topic + \"AND INNER JOIN \";\n    msg.topic = msg.topic + \";\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":840,"y":380,"wires":[["77006751.545558","1478fccf.a8a3d3"]]},{"id":"d2ca02c6.d422e","type":"json","z":"de4f6679.aaa758","name":"","property":"payload","action":"","pretty":false,"x":1250,"y":380,"wires":[["23afecf2.d72d14"]]},{"id":"6f674497.02751c","type":"debug","z":"de4f6679.aaa758","name":"DATABASE","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1050,"y":340,"wires":[]},{"id":"1478fccf.a8a3d3","type":"debug","z":"de4f6679.aaa758","name":"QUERY_ID","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":850,"y":340,"wires":[]},{"id":"e222ef82.8faae","type":"ui_template","z":"de4f6679.aaa758","group":"2efc921b.eeb7de","name":"Output_Table","order":1,"width":"18","height":"11","format":"<style>\n {\n    font-family: \"Trebuchet MS\", Arial, Helvetica, sans-serif;\n    border-collapse: collapse;\n    width: 100%;\n    postion: solid;\n}\n\ntd, th {\n    border: 1px solid #ddd;\n    padding: 8px;\n}\n\ntr:nth-child(even){background-color: #f2f2f2;}\n\ntr:hover {background-color: #ddd;}\n\nth {\n    padding-top: 12px;\n    padding-bottom: 12px;\n    text-align: left;\n    background-color: #2a71d4;\n    color: white;\n}\n</style>\n\n<table>\n    <tr>\n        <th>Time</th>\n        <th>Machine</th>\n        <th>Tag_ID</th>\n        <th>Value</th>\n        <th>Unit</th>\n        <th>Item_Serial_No</th>\n    </tr>\n  <tr ng-repeat=\"payload in msg.payload\">\n    <td>{{ payload.time }}</td>\n    <td>{{ payload.machine_name }} </td>\n    <td>{{ payload.name }} </td>\n    <td>{{ payload.value }} </td>\n    <td>{{ payload.units}}</td>\n    <td>{{ payload.item_serial_no }} </td>\n  </tr>\n</table>","storeOutMessages":false,"fwdInMessages":true,"templateScope":"local","x":1250,"y":420,"wires":[[]]},{"id":"23afecf2.d72d14","type":"debug","z":"de4f6679.aaa758","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1250,"y":340,"wires":[]},{"id":"c6044e3.2372bb","type":"inject","z":"de4f6679.aaa758","name":"","topic":"","payload":"Started!","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":"","x":220,"y":240,"wires":[["452ea09d.3dff1"]]},{"id":"1036ce2b.9809f2","type":"ui_button","z":"de4f6679.aaa758","name":"Button_ID_All","group":"9e579e53.ca3d","order":0,"width":"6","height":"1","passthru":false,"label":"Show all","color":"","bgcolor":"","icon":"","payload":" ","payloadType":"str","topic":"Button_Reset","x":440,"y":440,"wires":[["2f8e5fc5.9e2de"]]},{"id":"452ea09d.3dff1","type":"delay","z":"de4f6679.aaa758","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":230,"y":440,"wires":[["1036ce2b.9809f2","64318e66.b5cad"]]},{"id":"64318e66.b5cad","type":"subflow:70cb9a7a.7d32f4","z":"de4f6679.aaa758","name":"","x":370,"y":380,"wires":[["be714c06.a52f1"]]},{"id":"be714c06.a52f1","type":"ui_dropdown","z":"de4f6679.aaa758","name":"","label":"","place":"Select option","group":"9e579e53.ca3d","order":0,"width":0,"height":0,"passthru":true,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"","x":580,"y":380,"wires":[["2f8e5fc5.9e2de","52a3f69.79b2e08"]]},{"id":"52a3f69.79b2e08","type":"function","z":"de4f6679.aaa758","name":"SET_FLOW","func":"flow.set(\"machineId\",msg.payload);\nreturn msg;\n","outputs":1,"noerr":0,"x":610,"y":140,"wires":[["e0e22ca1.2bcbd"]]},{"id":"d858418c.b956d","type":"debug","z":"de4f6679.aaa758","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":770,"y":160,"wires":[]},{"id":"e0e22ca1.2bcbd","type":"function","z":"de4f6679.aaa758","name":"GET_FLOW","func":"var from_input = msg._fromInput;\n\n    var machineId = {payload: flow.get(\"machineId\")};\n    msg.machineId = machineId;\n\nreturn msg;","outputs":1,"noerr":0,"x":610,"y":180,"wires":[["d858418c.b956d","2f8e5fc5.9e2de"]]},{"id":"7838fe98.cc06a","type":"ui_template","z":"de4f6679.aaa758","group":"5b074627.dbdca8","name":"StartDate","order":0,"width":"6","height":"4","format":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <title>Bootstrap Example</title>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">\n  <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>\n  <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script>\n\n</head>\n<body>\n    \n    <form action=\"/ui\" method=\"post\">\n        <div id=\"date\">Begin:</div>\n        <input type=\"datetime-local\" name=\"begindate\" id=\"bdate\" value=\"2013-10-24T20:36:00\" step=\"1\">\n        <br>\n        <div id=\"date\">to End:</div>\n        <input type=\"datetime-local\" name=\"enddate\" id=\"edate\" value=\"2013-10-24T20:36:00\" step=\"1\">\n        <br><br>\n        <input type=\"submit\" id=\"submit\">\n      </form>\n      \n      <div id=\"divdiv\">L</div>\n\n<script src=\"./index.js\"></script>\n\n</body>\n</html>\n","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":300,"y":660,"wires":[["3898d89c.e7fbb8"]]},{"id":"ff37db30.247b08","type":"ui_template","z":"de4f6679.aaa758","group":"4f219b41.f1f344","name":"JS","order":0,"width":0,"height":0,"format":"<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script>\n<script>\n\n\"use strict\";\n\n        \n\n});\n\n\n    \n\n   function Rep(whenDate, index, replacement,elementID) {\n        var str = whenDate;\n        str = setCharAt(str,index,replacement);\n        document.getElementById(elementID).innerText = \"Time: \" + str;\n\n}\n\n    function setCharAt(str,index,chr) {\n        if(index > str.length-1) return str;\n        return str.substr(0,index) + chr + str.substr(index+1);\n}\n\n\ndocument.getElementById(\"submit\").addEventListener(\"click\", function(){\n\n    //event.preventDefault();\n\n    let beginDate = document.getElementById(\"bdate\").value;\n    let endDate = document.getElementById(\"edate\").value;\n\n\tRep(beginDate, 10, \" \",\"begindatetimetext\");\n    Rep(endDate, 10, \" \",\"enddatetimetext\");\n\n});\n//JQuery\n\n $(document).ready(function(){\n     \n    let dropdown__change = document.getElementById(\"select_value_label_0\");\n     \n    $(\"#submit\").click(function(){\n        $.post(\"/ui\",\n        {\n          dropdown__status: \n        },\n    });\n});\n\n\n\n</script>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":290,"y":620,"wires":[[]]},{"id":"3898d89c.e7fbb8","type":"http response","z":"de4f6679.aaa758","name":"","statusCode":"","headers":{},"x":430,"y":660,"wires":[]},{"id":"b1d800ef.d4a16","type":"http in","z":"de4f6679.aaa758","name":"","url":"/ui","method":"post","upload":false,"swaggerDoc":"","x":300,"y":700,"wires":[["88ad979d.dac068"]]},{"id":"88ad979d.dac068","type":"function","z":"de4f6679.aaa758","name":"REMOVE_T_FROM_DATE","func":"    msg.payload.begindate = {payload:msg.payload.begindate.replace(/T/g, \" \")};\n    msg.payload.enddate = {payload:msg.payload.enddate.replace(/T/g, \" \")};\n    return msg;\n    ","outputs":1,"noerr":0,"x":500,"y":700,"wires":[["ff18db4a.19e958"]]},{"id":"ff18db4a.19e958","type":"function","z":"de4f6679.aaa758","name":"SET_FLOW","func":"flow.set(\"begindate\",msg.payload.begindate);\nflow.set(\"enddate\",msg.payload.enddate);\n\nreturn msg;\n","outputs":1,"noerr":0,"x":650,"y":660,"wires":[["e0eb0b34.d41488"]]},{"id":"e0eb0b34.d41488","type":"function","z":"de4f6679.aaa758","name":"GET_FLOW","func":"msg.begindate = flow.get(\"begindate\")\nmsg.enddate = flow.get(\"enddate\")\nreturn msg;","outputs":1,"noerr":0,"x":670,"y":620,"wires":[["79bd9f2f.3fffa","2f8e5fc5.9e2de"]]},{"id":"79bd9f2f.3fffa","type":"debug","z":"de4f6679.aaa758","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":890,"y":600,"wires":[]},{"id":"6f90c35b.00d35c","type":"MySQLdatabase","z":"","host":"localhost","port":"","db":"pda","tz":""},{"id":"2efc921b.eeb7de","type":"ui_group","z":"de4f6679.aaa758","name":"Output_Table","tab":"f5df333.e4490d","order":4,"disp":true,"width":"18","collapse":false},{"id":"9e579e53.ca3d","type":"ui_group","z":"","name":"Button","tab":"f5df333.e4490d","disp":true,"width":"6","collapse":false},{"id":"5b074627.dbdca8","type":"ui_group","z":"","name":"Date","tab":"f5df333.e4490d","disp":true,"width":"6","collapse":false},{"id":"4f219b41.f1f344","type":"ui_group","z":"5c6013ad.23ba3c","name":"Output_Table","tab":"e4025f2e.5474c","order":4,"disp":true,"width":"18","collapse":false},{"id":"f5df333.e4490d","type":"ui_tab","z":"de4f6679.aaa758","name":"Attendorn","icon":"dashboard","order":1},{"id":"e4025f2e.5474c","type":"ui_tab","z":"5c6013ad.23ba3c","name":"Attendorn","icon":"dashboard","order":1}]

1 个答案:

答案 0 :(得分:0)

该错误在“ REMOVE_T_FROM_DATE”函数内部:

msg.payload.begindate = {payload:msg.payload.begindate.replace(/T/g, " ")};
msg.payload.enddate = {payload:msg.payload.enddate.replace(/T/g, " ")};
return msg;

您传入的msg.payload已经是一个对象,具有begindateenddate字段。在代码的前两行中,您将用另一个具有自己的有效负载的javascript对象替换这两个字符串(这就是{payload: xxx}语法的含义...用一个新的 new对象创建一个有效负载字段值为xxx)

相反,等号的右侧应返回一个新的 string ,该字符串表示日期和时间之间没有“ T”的数据字符串:

msg.payload.begindate = msg.payload.begindate.replace(/T/g, " ");
msg.payload.enddate = msg.payload.enddate.replace(/T/g, " ");
return msg;

这还意味着需要更改下游的“ Query_Id”函数,以从传入的msg对象的不同字段获取其数据:

var machine_id = msg.payload.machineId;
var begin_date = msg.payload.begindate;
var end_date = msg.payload.enddate;

仅供参考-综上所述,还有其他(更简单)的方法无需编写任何代码即可完成相同的操作。我喜欢使用node-red-contrib-moment节点格式化所有日期和时间。或者,如果您不想安装其他第3方节点,则可以使用核心change节点将“ T”替换为“”。

另一个建议是删除您的SET_FLOW / GET_FLOW函数对,并使用change节点来“设置:” flow. begindate“到” msg. {{ 1}},它复制从传入的味精到流上下文的开始日期,无需更改味精。这意味着您不必在设置流上下文值后立即检索它。