electron-storage icon indicating copy to clipboard operation
electron-storage copied to clipboard

storage.get

Open thardes2 opened this issue 10 years ago • 11 comments

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 :-)

thardes2 avatar Jul 29 '16 08:07 thardes2

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.

SchneiderOr avatar Jul 29 '16 08:07 SchneiderOr

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"}

thardes2 avatar Jul 29 '16 08:07 thardes2

Can you provide us the setting.json file in here?

SchneiderOr avatar Jul 29 '16 08:07 SchneiderOr

Here it is Settings.zip

thardes2 avatar Jul 29 '16 08:07 thardes2

Hi thardes2, there is an issue on Windows, I'll solve it as soon as I'll get to a computer!

ranyitz avatar Jul 29 '16 09:07 ranyitz

Hi @ran-y - Thank you very much :-)

thardes2 avatar Jul 29 '16 09:07 thardes2

Hi, @thardes2 I hope that it will solve the issue ! #https://github.com/Cocycles/electron-storage/commit/5b81c4d5f95eb48a0bc18146cf3d09ab2f48c942

ranyitz avatar Jul 29 '16 12:07 ranyitz

Thank you very much. This seems to solve the issue :-)

thardes2 avatar Jul 29 '16 13:07 thardes2

I got the same error on windows too, is the issue come back?

crakjie avatar Jun 07 '17 18:06 crakjie

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)

alexiusacademia avatar Oct 16 '17 08:10 alexiusacademia

Same error for me

gregoirepuget avatar Feb 02 '21 09:02 gregoirepuget