问题
If a user cancels the dialog box without selecting a file, the error ERR_INVALID_ARG_TYPE is thrown in console. The full error reads The "path" argument must be one of type string, Buffer, or URL. Received type object, which I believe is the case because undefined is returned.
This happens in at least the context of writeFile, readFile, and unlink.
I'm new to Node, and even newer to FS, so I'm wondering if there is an appropriate way to deal with this error other than ignoring it?
I have checked the documentation, but couldn't find anything referring specifically to this circumstance (i.e. a user cancelling the dialog box without selecting a file).
Example
delete file via unlink
document.querySelector('#delete-file').addEventListener('click', ()=>{
dialog.showOpenDialog((filename)=>{
if (filename === undefined){
console.log('No files were selected.')
return
}
if (!fs.existsSync(filename)){
alert(`The file, ${filename}, doesn't exist.`)
}
fs.unlink(filename, (err)=>{
if (err){
console.log(`An error occurred while updating the file: ${err.message}`)
return
}
alert('The file has been deleted.')
})
})
}, false)
来源:https://stackoverflow.com/questions/58199111/cancelled-dialog-box-without-selecting-file-err-invalid-arg-type