使用fs.readdir和fs.statSync返回ENOENT,没有此类文件或目录错误

use*_*679 3 fs node.js npm

这有效:

    var promise = new Future(),
        dirs = [],
        stat;


    Fs.readdir(Root + p, function(error, files){
        _.each(files, function(file) {
            //stat = Fs.statSync(file);
            //if ( stat.isDirectory() ) {
                dirs.push(file);
            //}
        });

        promise.return(dirs);
    });
Run Code Online (Sandbox Code Playgroud)

这不是:

    var promise = new Future(),
        dirs = [],
        stat;


    Fs.readdir(Root + p, function(error, files){
        _.each(files, function(file) {
            stat = Fs.statSync(file);
            if ( stat.isDirectory() ) {
                dirs.push(file);
            }
        });

        promise.return(dirs);
    });
Run Code Online (Sandbox Code Playgroud)

导致"错误:ENOENT,没有这样的文件或目录'字体'"

fonts是树中的第一个目录,它确实存在.

我一定会丢失一些愚蠢的东西.我正在尝试仅返回文件夹/目录名称.

虽然我在这里,有谁知道如何返回所有级别的目录?

例如,结果可能是:

[
    "fonts",
    "fonts/font-awesome",
    "images",
    "images/somepath",
    "images/somepath/anotherpath"
]
Run Code Online (Sandbox Code Playgroud)

在弄清楚我做错了什么之后,这是我的下一个目标.

我很感激帮助!

Zen*_*rbi 6

readdir将为您提供文件夹中条目的名称,而不是整个路径.这将有效:

stat = Fs.statSync(Root + p + "/" + file);
Run Code Online (Sandbox Code Playgroud)

整个代码:

var promise = new Future(),
    dirs = [],
    stat,
    fullPath;


Fs.readdir(Root + p, function(error, files){
    _.each(files, function(file) {
        fullPath = Root + p + "/" + file;
        stat = Fs.statSync(fullPath);
        if ( stat.isDirectory() ) {
            dirs.push(fullPath);
        }
    });

    promise.return(dirs);
});
Run Code Online (Sandbox Code Playgroud)