nodejs中fs.readFileSync()出错

时间:2015-10-18 15:14:12

标签: node.js filesystems fs

我在 nodejs

中使用 require('fs'); 调用同步通话
var folder_path = '/home/abc';
var myfiles = ['a.png','b.png'];
_.each(myfiles, function(name){
    var data = fs.readFileSync(folder_path+'/'+name);
    if(data){
       // Some operation
    }
});

在调用该函数时我正在错误:EISDIR,目录上的非法操作

fs.js:488
  var r = binding.read(fd, buffer, offset, length, position);
              ^
Error: EISDIR, illegal operation on a directory
at Object.fs.readSync (fs.js:488:19)
at Object.fs.readFileSync (fs.js:322:28)
at /home/coader/dev/api/controllers/MyController.js:66:30
at Function.forEach ( /home/coader/dev/node_modules/sails/node_modules/lodash/dist/lodash.js:3298:15)
at /home/coader/dev/api/controllers/MyController.js:41:11
at Object.oncomplete (fs.js:108:15)
error: Forever detected script exited with code: 8

详细

$ node -v
v0.10.37

$ nodejs -v
v0.10.37

$ npm -v
1.4.28

任何答案都会很明显。

由于

1 个答案:

答案 0 :(得分:0)

我认为你可能不得不满足于“myfiles.forEach(function(name)”

var folder_path = '/home/abc';
var myfiles = ['a.png','b.png'];
myfiles.forEach(function(name){
    var data = fs.readFileSync(folder_path+'/'+name);
    if(data){
             // Some operation
            }
});