nodejs表达fs将文件迭代到数组或对象失败 [英] nodejs express fs iterating files into array or object failing

查看:118
本文介绍了nodejs表达fs将文件迭代到数组或对象失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图使用nodejs express FS模块在我的应用程序中迭代一个目录,将每个文件名存储在一个数组中,我可以传递给我的快照视图并遍历列表,但是我努力这样做。当我在files.forEach函数循环中执行一个console.log时,它打印文件名就好了,但一旦我尝试做任何事情,如:

  var myfiles = []; 
var fs = require('fs');
fs.readdir('./ myfiles /',function(err,files){if(err)throw err;
files.forEach(function(file){
myfiles.push文件);
});
});
console.log(myfiles);

它失败,只记录一个空对象。所以我不知道究竟发生了什么,我认为它与回调函数有关,但是如果有人可以让我看看我做错了什么,为什么它不工作(以及如何使它工作),那将是非常感谢。

解决方案

myfiles数组是空的,因为在调用console.log()之前没有调用回调。



您需要执行以下操作:

  var fs = require('fs'); 
fs.readdir('./ myfiles /',function(err,files){
if(err)throw err;
files.forEach(function(file){
//为每个文件做一些事情HERE!
});
});
//因为尝试在这里执行文件的操作将无法正常工作,因为
//回调尚未触发。

记住,节点中的所有内容都发生在同一时间,在这个意义上,除非你在做您的回调内部的处理,您不能保证异步功能已经完成。



有关此问题的一种方法是使用EventEmitter:

  var fs = require('fs'),
EventEmitter = require('events')EventEmitter,
filesEE = new EventEmitter(),
myfiles = [];

//将所有文件添加到myfiles
filesEE.on('files_ready',function(){
console.dir(myfiles);
});

//从当前目录中读取所有文件
fs.readdir('。',function(err,files){
if(err)throw err;
files.forEach(function(file){
myfiles.push(file);
});
filesEE.emit('files_ready'); // trigger files_ready event
} );


So Im trying to use the nodejs express FS module to iterate a directory in my app, store each filename in an array, which I can pass to my express view and iterate through the list, but Im struggling to do so. When I do a console.log within the files.forEach function loop, its printing the filename just fine, but as soon as I try to do anything such as:

var myfiles = [];
var fs = require('fs');
fs.readdir('./myfiles/', function (err, files) { if (err) throw err;
  files.forEach( function (file) {
    myfiles.push(file);
  });
});
console.log(myfiles);

it fails, just logs an empty object. So Im not sure exactly what is going on, I think it has to do with callback functions, but if someone could walk me through what Im doing wrong, and why its not working, (and how to make it work), it would be much appreciated.

解决方案

The myfiles array is empty because the callback hasn't been called before you call console.log().

You'll need to do something like:

var fs = require('fs');
fs.readdir('./myfiles/',function(err,files){
    if(err) throw err;
    files.forEach(function(file){
        // do something with each file HERE!
    });
 });
 // because trying to do something with files here won't work because
 // the callback hasn't fired yet.

Remember, everything in node happens at the same time, in the sense that, unless you're doing your processing inside your callbacks, you cannot guarantee asynchronous functions have completed yet.

One way around this problem for you would be to use an EventEmitter:

var fs=require('fs'),
    EventEmitter=require('events').EventEmitter,
    filesEE=new EventEmitter(),
    myfiles=[];

// this event will be called when all files have been added to myfiles
filesEE.on('files_ready',function(){
  console.dir(myfiles);
});

// read all files from current directory
fs.readdir('.',function(err,files){
  if(err) throw err;
  files.forEach(function(file){
    myfiles.push(file);
  });
  filesEE.emit('files_ready'); // trigger files_ready event
});

这篇关于nodejs表达fs将文件迭代到数组或对象失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆