简体   繁体   中英

Why return doesn't work in NodeJS/Electron

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.

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.
}

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