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

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

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

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

References
#88

But mainly:
sindresorhus/conf#85

@leasontou
Copy link
Author

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

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

same bug guys

@EngJay
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants