lstatSync()返回未定义

问题描述 投票:0回答:1

我绝对不是Node.js的新手,我试图递归地删除文件夹中的所有文件,但是当使用lstatSync(path)时,它会一直返回未定义的状态。如果您好奇为什么这看起来很奇怪,而我使用的是Materialize和JSX,那是因为这是Electron应用程序的一部分。

错误显示:

Uncaught TypeError [ERR_INVALID_ARG_TYPE]:“ path”参数必须是字符串,Buffer或URL类型之一。在lstatSync收到的类型未定义]

const sendFiles = (files) => {
            for (let file of files) {
                const stat = fs.lstatSync(file.path);

                // if file is a folder, recursively check each file in the nested folders
                if (stat.isDirectory()) {
                    // rename for clarity
                    let files = fs.readdirSync(file.path);
                    sendFiles(files);
                } else {
                    // notify user about incompatible file types
                    if (
                        file.path.split(".").pop() !== "js" &&
                        file.path.split(".").pop() !== "css" &&
                        file.path.split(".").pop() !== "html" &&
                        file.path.split(".").pop() !== "svg"
                    ) {
                        //materialize toast
                        M.toast({
                            html: `"${file.path
                                .split("/")
                                .pop()}" could not be minified due to incompatible file type.`,
                        });
                    } else {
                        //send to main process for minification
                        ipcRenderer.send("file:add", file.path);
                        setRedir(<Redirect to="/list" />);
                    }
                }
            }
        };

这是调用函数的位置:

document.ondrop = (e) => {
            sendFiles(e.dataTransfer.files);
            e.preventDefault();
        };

出现此问题的原因是否普遍?就像我说的那样,我是Node的新手,所以这里有些事情可能已经完全浮现在我头上。

我绝对不是Node.js的新手,我试图递归地删除文件夹中的所有文件,但是当使用lstatSync(path)时,它会一直返回未定义的状态。如果您好奇为什么这看起来很奇怪...

javascript node.js fs
1个回答
0
投票

问题是e.dataTransfer.files返回file对象的数组,并在path属性中具有完整的文件名,但是fs.readdirSync返回的是仅文件名的数组,但没有其路径。

© www.soinside.com 2019 - 2024. All rights reserved.