Node js Glob 不返回文件

问题描述

我使用 glob 如下。

glob(
      `C:/master/temp/60dffca770d02959c568adb6_P+([0-9])_png.png`,null,(er,files) => {
        // Here not getting any file.
      },);

我确实有以下文件

C:\master\temp\60dffca770d02959c568adb6_P4_png.png

但没有文件返回响应。

我还在以下链接中测试了这种模式,它似乎也在那里工作。

https://www.digitalocean.com/community/tools/glob?comments=true&glob=C%3A%2FMaster%2FTemp%2F60dffca770d02959c568adb6_P%2B%28%5B0-9%5D%29_png.png&matches=false&tests=C%3A%2FMaster%2FTemp%2F60dffca770d02959c568adb6_P55_png.png

在这里做错了什么?

解决方法

当我在我的系统上使用差异模式运行时,它似乎工作正常。您提供的路径似乎不正确。使用的路径和示例路径的语法似乎不同。试试这个:

glob(
      `C:\master\temp\60dffca770d02959c568adb6_P+([0-9])_png.png`,null,(er,files) => {
        // Here not getting any file.
        console.log(files);
      },);
,

glob 仅用于匹配文件,但在实际读取文件内容时,需要与 glob 结合使用其他解决方案。

glob('C:\master\temp\60dffca770d02959c568adb6_P+([0-9])_png.png',function (err,files) {
        if (err) {
            console.log(err);
        } else {
            files.forEach(function (file) {
                fs.readFile(file,data) {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log(data.toString());
                    }
                });
            });
        }
    });
,

我找到了根本原因。这是我的错误。

实际上,长 GUID 让我很困惑,发现路径中实际上有不同的字符,尽管我一直保持不变。

代码没有错误。工作正常。只是我传递了错误的文档 ID,哪个文件不存在。