path must be a string or Buffer in the console: Node.js (Electron)

Asked

Viewed 679 times

0

I am developing an application on Electron, and one of my functions is to backup a json.

When I open the ('fs') and saved the file, I get no error on the console.

When opening the dialog box and canceling the operation (do not save the file) I get an error in the console:

Uncaught TypeError: path must be a string or Buffer
    at Object.fs.open (fs.js:549:11)
    at Object.module.(anonymous function) [as open] (ELECTRON_ASAR.js:200:20)
    at Object.fs.writeFile (fs.js:1211:6)
    at dialog.showSaveDialog (file:///C:/Users/Daniel/Documents/vendas-electron/app.js:63:10)
    at CallbacksRegistry.apply (C:\Users\Daniel\Documents\vendas-electron\node_modules\electron\dist\resources\electron.asar\common\api\callbacks-registry.js:48:42)
    at EventEmitter.<anonymous> (C:\Users\Daniel\Documents\vendas-electron\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:299:21)
    at emitThree (events.js:116:13)
    at EventEmitter.emit (events.js:194:7)

My backup function is this. What might be generating this error?

function backUp() {
  /* a) pega a data atual formatada */
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth()+1;
  var yyyy = today.getFullYear();
  if(dd<10){
      dd='0'+dd;
  }
  if(mm<10){
      mm='0'+mm;
  }
  var today = yyyy+'-'+mm+'-'+dd;
  /* FIM a) */

  var read = require('read-file-utf8') // Ler arquivos
  var dialog = remote.dialog // Carrega os dialogos do sistema
  var fs = require('fs'); // Carrega o File System (CRUD)
  const fileExists = require('file-exists')
  var check = fileExists.sync(__dirname+'/db.json')
  var data = {}
  if(check == true){
    data = read(__dirname+'/db.json')
    dialog.showSaveDialog({title: 'Backup do banco de dados',defaultPath: '.\\backup' + today +'.json'},(fileName) => {
      fs.writeFile(fileName, data, (err) => {
        if(err) console.log(err); // não é esta linha
        alert('O backup foi realizado com sucesso')
      })
    })
  } else{
    alert('Não há banco de dados para salvar.')
  }
}
  • What is the line vendas-electron/app.js? and what gives console.log(fileName); on the line before the fs.writeFile?

  • The console.log as undefined. I don’t understand the first question.

  • The first question was to find out which is line 63 of the file vendas-electron/app.js to try to understand where the error is being generated. But now I know that fileName gives undefined there’s your problem. What is dialog.showSaveDialog? This is browser or server code?

  • It is from the Electron: https://github.com/electron/blob/master/docs/api/dialog.md

  • To make it easier to lose the error you can try Try and Catch in the dialog function, as you may not be handling the case if it fails to save at the user’s order. as soon as you can send the result of catch thank you to try to help solve the problem :)

  • The error occurs in the fs.writeFile. While canceling save operation (standard dialog box to save windows files) I get this error. The error is entered in the question log: "path must be a string or Buffer"

Show 1 more comment

1 answer

0

The error occurs because the command fs.writeFile(fileName, data, (err) => { needs that fileName is a valid path, in case the action is being canceled, does not pass a valid path.

You can prevent this by validating the path before sending to fs.writeFile

Browser other questions tagged

You are not signed in. Login or sign up in order to post.