Run into a strange problem: Calling fs.stat() function inside Node-RED's function node, msg.payload can be updated correctly, depending on whether the file exists or does not exist. But the variable tt is undefined, instead of being 0 or 1. What could be the issue?
var tt;
fs.stat('/home/pi/testfile', function(err) {
if (err) {
msg.payload = false // testfile does not exist
tt=0;
} else {
msg.payload = true // testfile exists
tt=1;
}
});
msg.tt=tt; // tt is always undefined
return msg;
Please see the debug output:
The test flow is here:
[{"id":"d91f5f9882284a25","type":"function","z":"d3c8b30753c78d12","name":"Tests","func":"var tt;\n\nfs.stat('/home/pi/testfile', function(err) {\n if (err) {\n msg.payload = false // testfile does not exist\n tt=0;\n } else {\n msg.payload = true // testfile exists\n tt=1;\n }\n});\nmsg.tt=tt;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"fs","module":"fs"}],"x":1050,"y":1440,"wires":[["161e8c033aaf1a33"]]},{"id":"67ccbdbb6a93218e","type":"inject","z":"d3c8b30753c78d12","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":900,"y":1440,"wires":[["d91f5f9882284a25"]]},{"id":"161e8c033aaf1a33","type":"debug","z":"d3c8b30753c78d12","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1230,"y":1440,"wires":[]}]