close
The Wayback Machine - https://web.archive.org/web/20201024065448/https://github.com/sindresorhus/electron-store/issues/92
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaults not working #92

Open
leasontou opened this issue Dec 4, 2019 · 7 comments
Open

defaults not working #92

leasontou opened this issue Dec 4, 2019 · 7 comments

Comments

@leasontou
Copy link

@leasontou leasontou commented Dec 4, 2019

First, I set a defaults value when init the Storage object like this:

const defaults = {
  config: {
    node: {
      list: [
        'node1',
        'node2',
        'node3'
      ],
      current: 'node1'
    },
    language: 'en'
  }
}

const storage = new Storage({
  defaults
});

this default values will be generated after my app launches for the first time.
but then, I modify config.json manually, delete the value for key config.node, save file.
after that, content in config.json is:

{
  "config": {
    "language": "en"
  }
}

then I launch my app, storage.get("config.node") return undefined, I think it will regenerate the default value after I delete them, but it doesn't.

Is this a bug?

@Coding-Kiwi
Copy link

@Coding-Kiwi Coding-Kiwi commented Dec 4, 2019

I have the same issue, I have defined defaults and get undefined instead of the default value, then I tried defining a complete schema structure, same result.

cant provide a solution for now, just confirming

@Coding-Kiwi
Copy link

@Coding-Kiwi Coding-Kiwi commented Dec 4, 2019

quick and dirty workaround for now:

const dotProp = require('dot-prop');

const storage = new Storage({
  defaults
});

storage.getWorkaround = function (key, defaultValue) {
    let v = storage.get(key, defaultValue);
    return typeof v == "undefined" ? dotProp.get(storage._defaultValues, key, defaultValue) : v;
};
@Coding-Kiwi
Copy link

@Coding-Kiwi Coding-Kiwi commented Dec 4, 2019

References
#88

But mainly:
sindresorhus/conf#85

@leasontou
Copy link
Author

@leasontou leasontou commented Dec 5, 2019

quick and dirty workaround for now:

const dotProp = require('dot-prop');

const storage = new Storage({
  defaults
});

storage.getWorkaround = function (key, defaultValue) {
    let v = storage.get(key, defaultValue);
    return typeof v == "undefined" ? dotProp.get(storage._defaultValues, key, defaultValue) : v;
};

Thanks a lot, I get this solution.

@sindresorhus
Copy link
Owner

@sindresorhus sindresorhus commented Feb 22, 2020

Please see my comment in the original issue:

Can you open an issue on ajv and comment the link here?

Someone needs to do that.

@fedyfausto
Copy link

@fedyfausto fedyfausto commented May 11, 2020

same bug guys

@EngJay
Copy link

@EngJay EngJay commented May 27, 2020

As of 5.1.1 with electron 8.2.5, using the defaults key in a schema appears to work as expected but using default on an individual definition in the schema doesn't work at any level. I tried about every combination I could think of, including the workaround posted above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.