简体   繁体   English

Node js Glob 不返回文件

[英]Node js Glob returns no file

I am using glob as following.我正在使用glob如下。

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

And I do have file as following.我确实有以下文件。

C:\master\temp\60dffca770d02959c568adb6_P4_png.png

But no file is returning in response.但是没有文件返回响应。

I also tested this pattern in the following link and it seems to be working there as well.我还在以下链接中测试了这种模式,它似乎也在那里工作。

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 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&%2F60dffca770d02959c568adb6_P%2B%28%5B0-9%5D%29_png.png&matches=false&%2F60dffca770d02 2FMaster%2FTemp%2F60dffca770d02959c568adb6_P55_png.png

What am I doing wrong here?我在这里做错了什么?

It seems to work fine when I run on my system, with diff pattern.当我在我的系统上使用差异模式运行时,它似乎工作正常。 It seems the path you gave seems incorrect.您提供的路径似乎不正确。 The syntax seems different for the path used and the sample path.使用的路径和示例路径的语法似乎不同。 Try this:试试这个:

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

glob is just for matching files, but when it comes to actually reading the contents of the files and additional solution will need to be used in conjunction with glob. 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, function (err, data) {
                    if (err) {
                        console.log(err);
                    } else {
                        console.log(data.toString());
                    }
                });
            });
        }
    });

I found the root cause.我找到了根本原因。 It was an error from my end.这是我最后的错误。

Actually, long GUID confused me and found that actually there were different characters in path even though in question I had kept same.实际上,长GUID让我感到困惑,发现路径中实际上有不同的字符,尽管我一直保持不变。

There is no error in the code.代码中没有错误。 Working fine.工作正常。 Just I was passing the wrong document id for which file was not there.只是我传递了错误的文档 ID,其中文件不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM