简体   繁体   中英

Node js Glob returns no file

I am using glob as following.

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

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('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.

There is no error in the code. Working fine. Just I was passing the wrong document id for which file was not there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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