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

Docs missing how to deal with defaults for properties of an object (in schema) #102

Open
binaryfunt opened this issue Feb 14, 2020 · 4 comments

Comments

@binaryfunt
Copy link

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)
@binaryfunt
Copy link
Author

From sindresorhus/conf#85 (comment) 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?

@binaryfunt binaryfunt changed the title Schema object property defaults not working Docs missing how to deal with defaults for properties of an object Mar 1, 2020
@binaryfunt binaryfunt changed the title Docs missing how to deal with defaults for properties of an object Docs missing how to deal with defaults for properties of an object (in schema) Mar 1, 2020
@durasj
Copy link

durasj commented Mar 28, 2020

Thank you, @binaryfunt! Agreed, this is exactly what I was struggling with.

@borsTiHD
Copy link

Same problem here. Thanks for the help. :) 👍

@olsen4930824
Copy link

Gosh I've been debugging for 3 hours and here is the answer Thank you!

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

4 participants