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 givesconsole.log(fileName);
on the line before thefs.writeFile
?– Sergio
The console.log as
undefined
. I don’t understand the first question.– Daniel Bonifácio
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 thatfileName
givesundefined
there’s your problem. What isdialog.showSaveDialog
? This is browser or server code?– Sergio
It is from the Electron: https://github.com/electron/blob/master/docs/api/dialog.md
– Daniel Bonifácio
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 :)
– Pedro Monteiro Arantes
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"– Daniel Bonifácio