[英]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.