storage.get
I have the following code. ` var filePath = "Settings.json"
storage.set(filePath, "{'balance': 1000.21, 'num':100, 'is_vip':true, 'name':'foo'}")
.then(data => {
console.log(data);
})
.catch(err => {
console.error(err);
});
storage.get(filePath, (err, data) => {
if (err) {
console.error(err)
} else {
console.log(data);
}
});`
The data is written to the Settings.json under C:\Users\thardes2\AppData\Roaming\MyApp, but when I try to get them with the get function I get the following error:
The file in path C:\Users\thardes2\AppData\Roaming\MyApp/Settings.json is not a valid json file
So the format of the path is wrong. As you can see, I am just using the example provided in the README. Any idea or solution to this?
Thank you :-)
It might be caused by the double quotes in the setter. Try setting the json without double quotes. For example: storage.set('somepath.json', {prop1: 'value', prop2: 1234} .then(...)..
If its not the case, can you paste here the data of the json file after being set?
Cheers.
Tried it this way:
storage.set(filePath, {'balance': 1000.21, 'num':100, 'is_vip':true, 'name':'foo'}) .then(data => { console.log(data); }) .catch(err => { console.error(err); });
It still gives me the same error
The written data looks like this:
{"balance":1000.21,"num":100,"is_vip":true,"name":"foo"}
Can you provide us the setting.json file in here?
Here it is Settings.zip
Hi thardes2, there is an issue on Windows, I'll solve it as soon as I'll get to a computer!
Hi @ran-y - Thank you very much :-)
Hi, @thardes2 I hope that it will solve the issue ! #https://github.com/Cocycles/electron-storage/commit/5b81c4d5f95eb48a0bc18146cf3d09ab2f48c942
Thank you very much. This seems to solve the issue :-)
I got the same error on windows too, is the issue come back?
Hi there. I get the same error on macOS.
var data = new Object();
data.name = name;
data.address = address;
storage.set('customers.json', data)
.then(() => {
console.log('Data saved successfully!')
})
.catch((err) => {
console.log(err);
});
It writes to the file but then when I use the get function,
var oldData = storage.get('customers.json')
.then(d => {
console.log(d);
})
.catch(err => {
console.log(err);
});
It gives me that same error.
And here is what the console gives:
Error: The file in path /Users/syncster31/Library/Application Support/ASMS/customers.json is not a valid json file at /Users/syncster31/Documents/Programming/Electron/my-app/node_modules/electron-storage/dist/index…:37 at tryToString (fs.js:449) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:436)
Same error for me