electron-store
electron-store copied to clipboard
Docs missing how to deal with defaults for properties of an object (in schema)
I figured this out but will post the issue and show the solution in reply
const Store = require('electron-store');
const schema = {
foo: {
type: 'number',
default: 14
},
bar: {
type: 'object',
properties: {
a: {
type: 'number',
default: 5
}
}
}
}
const store = new Store({schema: schema});
console.log(store.get('foo'));
//=> 14
console.log(store.get('bar.a'));
//=> Cannot get property 'a' of undefined (I've paraphrased the error message)
From https://github.com/sindresorhus/conf/issues/85#issuecomment-531651424 the solution is to add default: {}
to bar
:
bar: {
type: 'object',
properties: {
a: {
type: 'number',
default: 5
}
},
default: {}
}
console.log(store.get('bar.a'));
//=> 5
Perhaps README could be updated to show this?
Thank you, @binaryfunt! Agreed, this is exactly what I was struggling with.
Same problem here. Thanks for the help. :) 👍
Gosh I've been debugging for 3 hours and here is the answer Thank you!