Node.js报错TypeError: Cannot read property 'isDirectory' of undefined

时间:2021-05-31 16:19:05

截图如下:

Node.js报错TypeError: Cannot read property 'isDirectory' of undefined

原因如下:记住"./uploads" 后要加一个/

 fs.stat("./uploads/" + files[i], function(err, stats) {

解决:

var fs = require("fs");

exports.getAllAlbums = function(callback) {

    // 找到所有文件夹
fs.readdir("./uploads", function(err,files) {
if (err) {
callback("没有找到uploads文件夹", null);
}
var allAlbums = [];
(function iterator(i) {
if (i == files.length) {
console.log(allAlbums);
// return allAlbums;
callback(null,allAlbums);
return;
}
fs.stat("./uploads/" + files[i], function(err, stats) {
if (err) {
callback("找不到文件"+files[i], null);
}
console.log(stats);
if (stats.isDirectory()) {
allAlbums.push(files[i]);
}
iterator(i + );
});
})();
});
// return ["小猫", "小狗"];
}