1

Using Node.js, I'm trying to search in the current directory for a specific file with a random number appended at the end of the filename. After some searching online I found this package would probably be best for the job, but I can't seem to make it work.

This is what I have so far:

const find = require('find');

function func1() {
    const x = find.file(/solution[0-9]*\.json/, __dirname, function(files) {
        console.log(files[0]);
        return files[0];
    });
    console.log(x);
}

func1();

This does a great job at finding the correct file and printing it to the console, but I still can't use the path to that file in func1, which is what I needed. This is the result I get:

{ error: [Function: error] }
C:\Users\dir1\dir2\solution78029853.json

I've tried multiple 'experimental' possible syntaxes but none of it seems to work and this is really the closest I can get. I did notice that the { error: [Function: error] } is displayed before the console.log(files[0]);, but I'm not really sure if that's the problem and if it is, I don't know how I can prevent it. Without the const x = and the console.log(x); I get no error, and only the path is displayed, so I'm sure something with that is wrong; I just don't know what. I've been searching the web for over an hour now with no progress so hopefully someone knows what (probably simple) thing I've been doing wrong.

Edit of func1() after Bergur's comment:

function func1() {
    const x = find.file(/solution[0-9]*\.json/, __dirname, function(files) {
        console.log(files[0]);
        return files[0];
    })
    .error(function(err) {
        if (err) {
            //
        }
    });
    console.log(x);
}

This doesn't produce the error, but just prints undefined instead for console.log(x);.

Hedde
  • 23
  • 5
  • Have you tried to use .error to see what error you are getting?: getting:https://www.npmjs.com/package/find#errorcallback – Bergur Nov 13 '19 at 23:21
  • I'm very much a newbie at JavaScript, which doesn't really help. I wasn't sure how to do this so I edited the question with the way I added it to my code... – Hedde Nov 13 '19 at 23:46
  • no problem, we all had to start somewhere. Just console.log(err) inside that and hopefully it should show you something. – Bergur Nov 14 '19 at 00:16
  • 1
    I believe you're running into this classic: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Khauri Nov 14 '19 at 00:47

3 Answers3

0

console.log(x) is not inside the func1() function

If you want to check the value of x, add a return statement inside the func1 function then console.log(func1())

PJ Concepcion
  • 108
  • 1
  • 13
  • Thank you for pointing that out, I edited the question so it now correctly represents the code I have so far. In the actual code however, `console.log(x)` is inside the `func1()` function so this should also work to check the value of x... (right?) – Hedde Nov 13 '19 at 23:19
0

This works 100%

//Try this
const util = require('util'); //https://nodejs.org/api/util.html
const findFileAsync = util.promisify(find.file);
const func1 = async () => {
  try{
    const results = await findFileAsync(/solution[0-9]*\.json/, __dirname);
    console.log(results[0]);
  } catch(err){
    console.log(err);
  }
}
func1();
Grisho4ek
  • 1
  • 1
0

The function find.file is asynchronous meaning that you will get the results in a callback (third argument of that function). Assuming you are using ES6 and want to avoid the callback hell problem, you could use async and await like this:

const find = require('find');

async function findFile(name, path) {
  return new Promise(function (ok, fail) {
    find.file(name, path, function(files) {
        ok(files.find(_ => true)) // resolve promise and return the first element
    });
  });
}

async function func1() {
    const x = await findFile(/testFile[0-9]/, "files")
    console.log(`Path to my file: ${x}`);
}

func1();

Link: https://repl.it/repls/ZestyJauntyApplicationserver