【问题标题】:Why return doesn't work in NodeJS/Electron为什么 return 在 NodeJS/Electron 中不起作用
【发布时间】:2021-11-12 23:25:16
【问题描述】:

我的 NodeJS 脚本有问题。 基本上我想将每个文件路径添加到一个数组中,然后在 bash 控制台中显示它。 但是当我尝试时,它给了我undefined

这是我的代码:

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());
})

它给了我这个输出而不是 tabRep 的元素:

未定义
.VERSIONS 是一个禁止的名称。
INSTALL 是一个禁止名称。

Programmes 文件夹内:

\程序

\.VERSIONS
\文件夹1
\文件1
\Folder2
\ 安装
\文件夹N
\文件N

如果有人能给我一些帮助,将不胜感激。

【问题讨论】:

标签: node.js electron


【解决方案1】:

fs.readdir() 期望回调函数作为第二个参数(您传递了该参数)。您指向的return 是回调函数的返回,而不是repList() 函数的返回。请阅读 JavaScript 中的异步函数和回调以完全理解这个概念,因为这在 JavaScript 中非常重要。此外,您的函数 repList() 不会返回任何内容!我认为缺少变量tabRep 的声明。

这么久了,fs.readdirSync()的同步变种,像这样:

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

【讨论】:

  • 非常感谢!这完美地工作。我肯定需要进一步研究同步/异步的事情。再次感谢!
猜你喜欢
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 2021-02-11
  • 1970-01-01
  • 2020-12-29
  • 2021-01-28
  • 1970-01-01
相关资源
最近更新 更多