Cancelled Dialog Box Without Selecting File: ERR_INVALID_ARG_TYPE

老子叫甜甜 提交于 2019-12-13 02:55:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!