electron-store
electron-store copied to clipboard
defaults not working
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?
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
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;
};
References https://github.com/sindresorhus/electron-store/issues/88
But mainly: https://github.com/sindresorhus/conf/issues/85
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.
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.
same bug guys
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.