-1

I have a problem with my NodeJS script. Basically I want to add every file path to an array then display it in the bash console. But when I try, it gives me undefined.

Here is my code:

const { app, BrowserWindow } = require('electron');
const fs = require('fs');
const path = require('path');

function repList(){
    var directoryPath = path.join('Q:/Programmes');
    let forbiddenDir = [".VERSIONS", "INSTALL"];
    fs.readdir(directoryPath, function (err, files) { //Scans the files in the directory
        if (err) {
            return console.log('Unable to scan directory: ' + err);
        }
        else{
            files.forEach(function (file){ //Loops through each file
                var name = directoryPath+"/"+file;
                if(forbiddenDir.includes(file)){ //Don't accept the file if unvalid
                    console.log(`${file} is a forbidden name.`);
                }
                else{ //Filename is valid
                    fs.stat(name, (error, stats) => {
                        if (stats.isDirectory()) { //If directory...
                            tabRep.push(name); //... add the full filename path to the tabRep array
                        }
                        else if (error) {
                            console.error(error);
                        }
                    });
                };
            }); //End of loop
            return tabRep; //<-- THIS RETURN DOESN'T WORK
        }
    });
}

app.whenReady().then(() => {
    console.log(repList());
})

It gives me this output instead of tabRep's elements:

undefined
.VERSIONS is a forbidden name.
INSTALL is a forbidden name.

Inside the Programmes folder :

\ Programmes

\ .VERSIONS
\ Folder1
\ File1
\ Folder2
\ INSTALL
\ FolderN
\ FileN

If anyone could give me some help, it would be really appreciated.

Newend
  • 1
  • 1
    Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – E_net4 Nov 12 '21 at 15:15

1 Answers1

0

fs.readdir() expects a callback function as second parameter (you passed that). The return you point at is the return of the callback function - not the return of the repList() function. Please read about async functions and callbacks in JavaScript to fully understand this concept, as this is very important in JavaScript. Also, your function repList() does not return anything! And declaration of variable tabRep is missing I think.

For so long, the the synchronous variant of fs.readdirSync(), like so:

const { app, BrowserWindow } = require('electron');
const fs = require('fs');
const path = require('path');

function repList(){
    var directoryPath = path.join('Q:/Programmes');
    let forbiddenDir = [".VERSIONS", "INSTALL"];
    const files = fs.readdirSync(directoryPath)
    const tabRep = []

    files.forEach(function (file){ //Loops through each file
        var name = directoryPath+"/"+file;
        if(forbiddenDir.includes(file)){ //Don't accept the file if unvalid
            console.log(`${file} is a forbidden name.`);
        }
        else{ //Filename is valid
            const stats = fs.statSync(name)
            if (stats.isDirectory()) { //If directory...
                tabRep.push(name); //... add the full filename path to the tabRep array
            }
        }
    }); //End of loop
    return tabRep; //<-- THIS RETURN DOES WORK NOW since now the function executes synchronously.
}
krassdanke
  • 617
  • 6
  • 24
  • Thank you so much! This works perfectly. I definitely need to seek further into the sync/async thing. Thanks again! – Newend Nov 15 '21 at 08:07