1

Could someone please explain how do I properly use this JS module https://github.com/Cocycles/electron-storage

Created json file:

{
  "test": []
}

Question:

I've created the file by calling storage.set(filePath, {"test": []}) but how do I change the properties now?

For example, how would I add string "path1" into the "test" property of that json file?

When I do the following, it just replaces everything in the file:

storage.get(filePath)
.then(data => { storage.set(filePath, data["test"] = "path1") }
Un1
  • 3,250
  • 7
  • 28
  • 65
  • google [electron-storage](https://www.google.com/search?q=electron-storage&rlz=1C5CHFA_enUS688US688&oq=electron-storage&aqs=chrome.0.0j69i60j0l4.2911j1j7&sourceid=chrome&ie=UTF-8) – SaganRitual Mar 23 '18 at 00:49
  • You need to do some research and try to do it yourself before you post a question. See the [guidelines](https://stackoverflow.com/help/how-to-ask) for asking questions. – SaganRitual Mar 23 '18 at 00:53
  • @GreatBigBore sorry, I couldn't find the answer in Google – Un1 Mar 23 '18 at 00:55
  • You read **all** of these and couldn't find anything helpful? [How to store user data in Electron](https://codeburst.io/how-to-store-user-data-in-electron-3ba6bf66bc1e), [How to persist data in an electron app](https://stackoverflow.com/questions/35660124/how-to-persist-data-in-an-electron-app) [Is there any local storage for electron?](https://discuss.atom.io/t/is-there-any-localstorage-for-electron/40486) If you have read them, then you need to discuss, in your question, what you've read and why it didn't work for you. See the guidelines. – SaganRitual Mar 23 '18 at 00:57
  • @GreatBigBore well, I always try to research first, but I didn't find the solution. The localStorage is a different thing, I was having troubles understanding the specified in the question module. – Un1 Mar 23 '18 at 01:02
  • That's fine. But you need to discuss that in your question. You need to show that you made a reasonable effort. And you didn't tell me whether you read all three of those links I gave you, which makes me suspect that you didn't. – SaganRitual Mar 23 '18 at 01:03

1 Answers1

1

you'll have to get the variable, update it and then put it back again, try this :

storage.get(filePath)
    .then(data => {
        data.test.push("path1"); // this will add "path1" to your array and not overwrite it
        storage.set(filePath, data)
    })
    .catch(err => {
      console.error(err);
    });

according to your edit : what you needed is to add "path1" to the array test with push

Taki
  • 15,354
  • 3
  • 20
  • 39
  • Thank you for the answer, that works indeed. How do I remove added item though? The regular `index` + `slice` technique? – Un1 Mar 23 '18 at 00:51
  • yup, get the variable, edit it ( add remove .. ) then set it back , https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript – Taki Mar 23 '18 at 00:52
  • Works like a charm, thanks for your help! By the way, in your opinion is it better to use this module since it's using promises, or I can just use [this](https://github.com/sindresorhus/electron-store) simpler module which allows you to add an item in 1 line: `store.set('foo', true);` and remove it in 1 line as well `store.delete('foo');` Are promises really that much better that you should sacrifice simpler solutions? – Un1 Mar 23 '18 at 01:08
  • welcome :) glad i could help , the one you're using can be used without promises and removes an item with one line like `storage.remove(path)` but if for updating the variable you need to `get` it and update it and set it back for both of them, i would go for this one personally, i love promises :P , and it's useful to `catch` errors or if you're dealing with asynchronous stuff – Taki Mar 23 '18 at 01:19
  • 1
    Good to know! Yeah, I suppose it's better to use promises to be safe – Un1 Mar 23 '18 at 01:31