![](/img/trans.png)
[英]TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined and code: 'ERR_INVALID_ARG_TYPE'
[英]fs.FileRead -> TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type undefined
function openFileDialog() {
dialog.showOpenDialog(win, {
properties: ['openFile']
} , filepath => {
if (filepath) {
fs.writeFile('path.txt', filepath, function (err, data) {
if (err) console.log(err);
});
scanFile(filepath)
}
})
}
function scanFile(filepath) {
if(!filepath || filepath[0] == 'undefined') return;
console.log(filepath)
fs.readFile(filepath,"utf8", (err,data) => { // ----> *ERROR*
if(err) console.log(err);
var arr = [];
if (data.substr(-4) === '.mp3' || data.substr(-4) === '.m4a'
|| data.substr(-5) === '.webm' || data.substr(-4) === '.wav'
|| data.substr(-4) === '.aac' || data.substr(-4) === '.ogg'
|| data.substr(-5) === '.opus') {
arr.push(files[i]);
}
var objToSend = {};
objToSend.files = arr;
objToSend.path = filepath;
win.webContents.send('selected-files', objToSend)
})
}
我试图制作电子音乐播放器应用程序。 第一步是打开我的文件。 当我打开文件时,"TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be one of type string, Buffer, or URL. Received type undefined" 错误发生并且错误消息显示 scanFile(filepath), fs.readFile(~ ~) 导致错误。 我该如何解决?
scanFile
的第一行是:
if(!filepath || filepath[0] == 'undefined') return;
这向我表明filepath
是一个数组,而不是一个字符串(或缓冲区或 URL)。 检查console.log
语句的输出以查看是否是这种情况。 由于if
语句正在检查filepath[0]
,我将从那里开始并更新代码以读取fs.readFile(filepath[0],"utf8", (err,data) => {
,因为if
语句暗示该filepath[0
] 是您应该使用的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.