繁体   English   中英

files.forEach(function(file){^ TypeError:无法读取未定义的属性'forEach'

[英]files.forEach(function(file) { ^ TypeError: Cannot read property 'forEach' of undefined

var testFolder = '/zip_file\ /sit1_Wave2_Settlement_afx_formula\ \
(1\)/data_dictionary/CM.173/';
   var fs = require('fs');
      fs.readdir(testFolder, (err, files) => {
        files.forEach(file => {
    console.log(file);

});
})

我需要读取文件内容,如果我执行上述代码,则会显示错误。

files.forEach(function(file) {
   ^

TypeError: Cannot read property 'forEach' of undefined

在尝试读取文件之前,应检查错误,然后检查是否定义了files对象,如下所示:

fs.readdir(testFolder, (err, files) => {
    if (err) throw err;
    if (files){
        files.forEach(file => {
            console.log(file);
        });
    } else {
        console.log('no files found')
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM