Why .. "TypeError: Cannot read property 'apply' of undefined"

var States = 1;
var msg1 = {payload : msg.payload};
msg1.States = States;

function delay(){
msg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': 400 , 'quantity': 2 } ;
}
setTimeout(delay(),200 );

msg1.States = msg1.States+1;
return [msg,msg1];
//
[{"id":"1636ba76.82acf6","type":"function","z":"dd5f2f8f.3da71","name":"Write on FC16 ID1 A400 Q2","func":"var States = 1;\nvar msg1 = {payload : msg.payload};\nmsg1.States = States;\n\nfunction delay(){\n msg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': 400 , 'quantity': 2 } ;\n}\nsetTimeout(delay(),200 );\n\nmsg1.States = msg1.States+1;\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1820,"y":1000,"wires":[["d15a28c6.2204a8"],]},{"id":"429b0bb8.487d64","type":"change","z":"dd5f2f8f.3da71","name":"OpenClamp","rules":[{"t":"set","p":"payload","pt":"msg","to":"[0, 3]","tot":"bin"},{"t":"set","p":"topic","pt":"msg","to":"OpenClamp","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1590,"y":940,"wires":[["3cdd9c85.dc2e54"]]},{"id":"9f76a06b.34fc6","type":"change","z":"dd5f2f8f.3da71","name":"OpenCut","rules":[{"t":"set","p":"payload","pt":"msg","to":"[0, 1]","tot":"bin"},{"t":"set","p":"topic","pt":"msg","to":"OpenCut","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1580,"y":1000,"wires":[["1636ba76.82acf6"]]},{"id":"9c1d5497.a2c228","type":"change","z":"dd5f2f8f.3da71","name":"AbsPX0","rules":[{"t":"set","p":"payload","pt":"msg","to":"[0, 0]","tot":"bin"},{"t":"set","p":"topic","pt":"msg","to":"AbsPX0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1580,"y":1280,"wires":[["6a46420b.83fccc"]]},{"id":"dcace127.86fd3","type":"function","z":"dd5f2f8f.3da71","name":"Write on FC16 ID1 A200 Q2","func":"var States = 1;\nvar msg1 = {payload : msg.payload};\nmsg1.States = States;\n\nfunction delay(){\n msg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': 200 , 'quantity': 2 } ;\n}\nsetTimeout(delay(),300 );\n\nmsg1.States = msg1.States+1;\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1820,"y":1080,"wires":[["d15a28c6.2204a8"],]},{"id":"c4e78e5a.7a7a8","type":"inject","z":"dd5f2f8f.3da71","name":"MoveAbsP1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1330,"y":1080,"wires":[["429b0bb8.487d64","9f76a06b.34fc6","5ffcc3d7.1108ec","2fe497b1.12d8b8","ceacb568.e13d88","9c1d5497.a2c228"]]},{"id":"5ca5cd18.061874","type":"function","z":"dd5f2f8f.3da71","name":"Write on FC16 ID1 A400 Q2","func":"var States = 1;\nvar msg1 = {payload : msg.payload};\nmsg1.States = States;\n\nfunction delay(){\n msg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': 400 , 'quantity': 2 } ;\n}\nsetTimeout(delay(),4000 );\n\nmsg1.States = msg1.States+1;\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1820,"y":1160,"wires":[["d15a28c6.2204a8"],]},{"id":"c42645a1.4004f8","type":"function","z":"dd5f2f8f.3da71","name":"Write on FC16 ID1 A400 Q2","func":"var States = 1;\nvar msg1 = {payload : msg.payload};\nmsg1.States = States;\n\nfunction delay(){\n msg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': 400 , 'quantity': 2 } ;\n}\nsetTimeout(delay(),4200 );\n\nmsg1.States = msg1.States+1;\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1820,"y":1220,"wires":[["d15a28c6.2204a8"],]},{"id":"2fe497b1.12d8b8","type":"change","z":"dd5f2f8f.3da71","name":"CloseClamp","rules":[{"t":"set","p":"payload","pt":"msg","to":"[0, 4]","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":1570,"y":1160,"wires":[["5ca5cd18.061874"]]},{"id":"ceacb568.e13d88","type":"change","z":"dd5f2f8f.3da71","name":"CloseCut","rules":[{"t":"set","p":"payload","pt":"msg","to":"[0, 2]","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":1580,"y":1220,"wires":[["c42645a1.4004f8"]]},{"id":"5ffcc3d7.1108ec","type":"change","z":"dd5f2f8f.3da71","name":"AbsPY1","rules":[{"t":"set","p":"payload","pt":"msg","to":"[0,1]","tot":"bin"}],"action":"","property":"","from":"","to":"","reg":false,"x":1580,"y":1080,"wires":[["dcace127.86fd3"]]},{"id":"3cdd9c85.dc2e54","type":"function","z":"dd5f2f8f.3da71","name":"Write on FC16 ID1 A400 Q2","func":"var States = 1;\nvar msg1 = {payload : msg.payload};\nmsg1.States = States;\n\nfunction delay(){\n msg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': 400 , 'quantity': 2 };\n}\nsetTimeout(delay(),100 );\n\nmsg1.States = msg1.States+1;\nreturn [msg,msg1];\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1820,"y":940,"wires":[["d15a28c6.2204a8"],]},{"id":"6a46420b.83fccc","type":"function","z":"dd5f2f8f.3da71","name":"Write on FC16 ID1 A100 Q2","func":"var States = 1;\nvar msg1 = {payload : msg.payload};\nmsg1.States = States;\n\nfunction delay(){\n msg.payload = { value: msg.payload, 'fc': 16, 'unitid': 1, 'address': 400 , 'quantity': 2 } ;\n}\nsetTimeout(delay(),5000 );\n\nmsg1.States = msg1.States+1;\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1820,"y":1280,"wires":[["d15a28c6.2204a8"],]},{"id":"d15a28c6.2204a8","type":"modbus-flex-write","z":"dd5f2f8f.3da71","name":"","showStatusActivities":false,"showErrors":false,"server":"dae9db0c.a6ca78","emptyMsgOnFail":true,"keepMsgProperties":false,"x":2230,"y":660,"wires":[["647add9.4e14724"],["4715ef3e.28d4c","dfe5db6c.a309e8"]]},{"id":"647add9.4e14724","type":"debug","z":"dd5f2f8f.3da71","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2590,"y":640,"wires":},{"id":"4715ef3e.28d4c","type":"debug","z":"dd5f2f8f.3da71","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2570,"y":680,"wires":},{"id":"dae9db0c.a6ca78","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"10502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

Hi, in order to make your flow importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.

See this post for more details - How to share code or flow json

1 Like

Also it is not clear what you are trying to show. You show a function with apparently another copy of the function nested inside it. Please edit the post (you can click on the pencil next to it) and correct it.

However, looking at the function I think you may be confused about what setTimeout() does. Where you have

setTimeout(delay(),200 );
msg1.States = msg1.States+1;
return [msg,msg1];

In the call to setTimeout you should specify the function name, without the parentheses, so it should be
setTimeout(delay,200 );
but also that tells node red that in 200ms it should call the delay function, but then the inline execution immediately continues so it will immediately set msg1.States and return with the original msg unchanged. Then in 200 ms it will call the delay function which will change msg.payload, but that will change the payload of the message that was returned 200ms ago and that message will either have been destroyed by then or if it is is still flowing through nodes further along the flow then its payload will be changed suddenly.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.