【发布时间】:2020-10-02 18:20:51
【问题描述】:
我绝对是 Node.js 的新手,我正在尝试以递归方式删除文件夹中的所有文件,但是在使用 lstatSync(path) 时,它一直返回未定义。如果你好奇为什么这看起来很奇怪,而我正在使用 Materialize 和 JSX,那是因为这是 Electron 应用程序的一部分。
错误显示:
未捕获的 TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串、缓冲区或 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 新手,所以这里的某些事情可能完全超出我的想象。
【问题讨论】:
标签: javascript node.js fs