-3
以下Node.js代码打印'undefined',即使它找到了该文件。Node.js函数返回'undefined'
var fileFound = function() {
fs.readFile('public/images/acphotos/Friedrich-EL36N35B.jpg', function(err, data) {
if (err) {
console.log(err);
return false;
} else {
return true;
}
});
}
console.log("Return value: " + fileFound());
我该如何重写它?我没有完全理解我所展示的其他线程中的解决方案。
'fs.readfile'是***异步***! – adeneo
你的'fileFound'函数不返回任何东西。 – SteamDev
每个函数都有自己的返回值。 'fileFound'函数返回的任何内容都不代表_undefined_。 – undefined