Cannot read property 'isDirectory' of undefined

preliminary learning Node, keeps reporting Cannot read property "isDirectory" of undefined, when doing a read about files, judging whether the type of each file is a file directory, and then I try to use isFile, to also report undefined

related codes

function file(i) {
  var filename = filesarry[i];
  fs.stat(__dirname + "/" + filesarry[i], function(err, stat) {
    stats[i] = stat;
    if (stat.isDirectory()) {
      console.log("   " + i + "   \033[36m" + filesarry[i] + "/\033[39m");
    } else {
      console.log("   " + i + "   \033[90m" + filename + "/\033[39m");
    }
    iPP;
    if (i == filesarry.length) {
      read();
    } else {
      file(i);
    }
  });
}

:
C:\Users\DZY\Desktop\Node\\file-explorer\index.js:28
    if (stat.isDirectory()) {
             ^

TypeError: Cannot read property "isDirectory" of undefined
    at C:\Users\DZY\Desktop\Node\\file-explorer\index.js:28:14
    at FSReqWrap.oncomplete (fs.js:152:21)

I have looked up the data everywhere but failed to solve the problem. Beginners of Node, hope to have some advice from the gods who know why

Feb.23,2022

it is possible that an exception was thrown, but you did not catch the exception, so the stat value is undefined.

you can see what the value of err is

.
 fs.stat(__dirname + "/" + filesarry[i], function(err, stat) {
    if (err) {
        throw err
        return false;
    }
    stats[i] = stat;
   // ... 
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ead28e-2131.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ead28e-2131.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?