简体   繁体   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);

});
})

I need to read a file contents, if i execute the above code it shows an error. 我需要读取文件内容,如果我执行上述代码,则会显示错误。

files.forEach(function(file) {
   ^

TypeError: Cannot read property 'forEach' of undefined

You should check for errors before trying to read the files, and then check to see if the files object is defined, like this: 在尝试读取文件之前,应检查错误,然后检查是否定义了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.

相关问题 类型错误:无法读取未定义的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“forEach” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined 错误:TypeError:无法读取未定义的属性“forEach” - Error : TypeError: Cannot read property 'forEach' of undefined TypeError:无法读取未定义 React 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined React 类型错误:无法读取未定义 Javascript 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined Javascript 类型错误:无法读取未定义的属性“forEach”,javascript - TypeError: Cannot read property 'forEach' of undefined , javascript forEach 数组出现错误类型错误:无法读取未定义的属性“forEach” - forEach array getting error TypeError: Cannot read property 'forEach' of undefined 未捕获(承诺)TypeError:无法读取未定义的属性“ forEach” - Uncaught (in promise) TypeError: Cannot read property 'forEach' of undefined Firebase未捕获的TypeError:无法读取未定义的属性'forEach' - Firebase Uncaught TypeError: Cannot read property 'forEach' of undefined 类型错误:无法读取未定义的属性“forEach”(节点 js、stripe、express) - TypeError: Cannot read property 'forEach' of undefined (node js, stripe, express)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM