Javascript Can';t使用Store.js保存/创建文件
所以我想使用Store.js在客户机存储上保存一个文件 我可以使用Javascript Can';t使用Store.js保存/创建文件,javascript,reactjs,save,electron,store,Javascript,Reactjs,Save,Electron,Store,所以我想使用Store.js在客户机存储上保存一个文件 我可以使用store.set更改日期,我可以log将其记录到控制台以查看更改,但之后它应该保存在未创建的应用程序数据中 我试图找到保存它的路径,它是: C:\Users\USER\AppData\Roaming\2/Categories.json 我注意到有一个“/”所以我试着: C:\Users\USER\AppData\Roaming\toma2\Categories.json 以及: C:/Users/USER/AppData/Roa
store.set
更改日期,我可以log
将其记录到控制台以查看更改,但之后它应该保存在未创建的应用程序数据中
我试图找到保存它的路径,它是:
C:\Users\USER\AppData\Roaming\2/Categories.json
我注意到有一个“/”所以我试着:
C:\Users\USER\AppData\Roaming\toma2\Categories.json
以及:
C:/Users/USER/AppData/Roaming/2/Categories.json
但这三个都不起作用
这是my Store.js:
const fs = require('browserify-fs');
var fs2 = require('filereader'),Fs2 = new fs2();
const electron = window.require('electron');
const path = require('path');
class Store {
constructor(opts) {
// Renderer process has to get `app` module via `remote`, whereas the main process can get it directly
// app.getPath('userData') will return a string of the user's app data directory path.
//const userDataPath = (electron.app || electron.remote.app).getPath('userData');
var userDataPath = (electron.app || electron.remote.app).getPath('userData');
for(var i=0;i<userDataPath.length;i++){
if(userDataPath.charAt(i)=="\\"){
userDataPath = userDataPath.replace("\\","/");
}
}
// We'll use the `configName` property to set the file name and path.join to bring it all together as a string
this.path = path.join(userDataPath, opts.configName + '.json');
this.data = parseDataFile(this.path, opts.defaults);
console.log(this.path);
}
// This will just return the property on the `data` object
get(key) {
return this.data[key];
}
// ...and this will set it
set(key, val) {
this.data[key] = val;
// Wait, I thought using the node.js' synchronous APIs was bad form?
// We're not writing a server so there's not nearly the same IO demand on the process
// Also if we used an async API and our app was quit before the asynchronous write had a chance to complete,
// we might lose that data. Note that in a real app, we would try/catch this.
fs.writeFile(this.path, JSON.stringify(this.data));
}
}
function parseDataFile(filePath, data) {
// We'll try/catch it in case the file doesn't exist yet, which will be the case on the first application run.
// `fs.readFileSync` will return a JSON string which we then parse into a Javascript object
try {
return JSON.parse(Fs2.readAsDataURL(new File(filePath)));
} catch(error) {
// if there was some kind of error, return the passed in defaults instead.
return data;
}
}
// expose the class
export default Store;
现在,如果可能的话,我可能需要找到另一种保存文件的方法
我尝试了:fs:由于某些原因,它对我不起作用(我会遇到奇怪的错误,它们不想被修复..)
如果有人有什么想法,我将不胜感激。所以我设法解决了这个问题,为什么fs向我发送有关未定义函数的错误?为什么没有创建文件?它本身与代码没有任何关系,但是导入 为了澄清,我使用了:
const fs=require('fs')代码>
解决方案是使其像:
const fs=window.require('fs')代码>
只需添加窗口。
修复了所有问题。由于这是我第一次使用electron,我不习惯从窗口导入,但这似乎是必要的。此外,没有帖子说这是修复方法。你在不理解的情况下剪切和粘贴代码。再次阅读我昨天发布的教程链接。这并不是我不理解它,当我用普通的fs api尝试它时,我得到了关于函数不存在的奇怪错误,所以我使用了browserify fs
来对抗错误,但这似乎并不能解决问题。(TypeError:fs.writeFileSync不是函数)
//creation
const storeDefCat = new Store({
configName: "Categories",
defaults: require("../data/DefaultCategorie.json")
})
//call for the save
storeDefCat.set('Pizza',{id:0,path:storeDefCat.get('Pizza').path});