Tuesday, 30 May 2017

Nodejs, electron: How do I write a legal path to file using fs?

This is my nodejs(electron) code:

var fs = require('fs');
var path = require('path');


var _newPath = dialog.showOpenDialog({properties: ['openDirectory']});//Get the path

_newPath = _newPath[0];
console.log(_newPath);
_newPath = path.join(_newPath, "filesystem.json");
_newPath = path.normalize(_newPath);

console.log(_newPath);

When I later on in the code write the data to a .JSON file:

fs.writeFile(_path, "[\"path\":\"" + _newPath + "\]", (err) => {
  if (err) throw err;
});

the filepath is wrong:

"path":"D:\json\filesystem.json"

(The :es becomes "\j", "\f")

How do I make this path a "legal" string?



via Qoutroy

No comments:

Post a Comment