lutzer / node-red-contrib-finite-statemachine

A finite state machine implementation for node red.
MIT License
16 stars 5 forks source link

Sync does not work when using a function node #24

Open Landstein opened 2 years ago

Landstein commented 2 years ago

When setting the state using sync it only works using the inject node. Using any other notes to pass the data gets an error

{"msg":"this._state.toJS is not a function"}

lutzer commented 2 years ago

could you post an example? I just tried it out and i do not have the same problem.

michael-borkowski commented 1 year ago

Hi, I have the exact same issue. I'm not sure what the best way to show the example is, but I have the following example flow that demonstrates the issue:

[{"id":"67752b44341d2fef","type":"finite-state-machine","z":"12873024fdd5de26","name":"","fsmDefinition":"{\"state\":{\"status\":\"IDLE\",\"data\":{\"x\":5}},\"transitions\":{\"IDLE\":{\"run\":\"RUNNING\"},\"RUNNING\":{\"stop\":\"IDLE\"}}}","sendInitialState":false,"sendStateWithoutChange":false,"showTransitionErrors":true,"x":870,"y":500,"wires":[["7127f5221b2b31fd"]]},{"id":"c75957730834da98","type":"inject","z":"12873024fdd5de26","name":"","props":[{"p":"control","v":"sync","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"status\": \"RUNNING\" }","payloadType":"json","x":580,"y":440,"wires":[["67752b44341d2fef"]]},{"id":"7127f5221b2b31fd","type":"debug","z":"12873024fdd5de26","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1070,"y":500,"wires":[]},{"id":"d1195305ed62b705","type":"inject","z":"12873024fdd5de26","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":430,"y":500,"wires":[["11991b912eac14a1"]]},{"id":"11991b912eac14a1","type":"function","z":"12873024fdd5de26","name":"function 4","func":"msg = {\n    control: \"sync\",\n    payload: {\n        status: \"RUNNING\"\n    }\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":500,"wires":[["67752b44341d2fef"]]},{"id":"4fc52c5e695266ea","type":"inject","z":"12873024fdd5de26","name":"","props":[{"p":"control","v":"sync","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"status\": \"IDLE\" }","payloadType":"json","x":570,"y":400,"wires":[["67752b44341d2fef"]]}]

Clicking the two first inject nodes properly syncs the FSM. Clicking the final one (which triggers the function) causes the error:

09/12/2022, 19:04:43[node: 67752b44341d2fef](http://<IP>:1880/#)
msg : Object
object
code: undefined
msg: "this._state.toJS is not a function"

It sounds like it might be happening here but it could also be anywhere else, of course.

I'm using Node-RED v3.0.2 and node-red-contrib-finite-statemachine@2.1.3 (in Docker but I don't think that plays a role). Let me know if I can provide more details :) Thanks for this work, in any case!

heliart-steve commented 1 year ago

I also have this exact same issue. Inject works just fine, but always throws this error from a function. I can post a simplified example if it help.

I'm running NodeRED 3.0.2 node-red-contrib-finite-statemachine 2.1.3

Cheers.