electron-store icon indicating copy to clipboard operation
electron-store copied to clipboard

defaults not working

Open leasontou opened this issue 5 years ago • 7 comments

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?

leasontou avatar Dec 04 '19 09:12 leasontou

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 avatar Dec 04 '19 18:12 Coding-Kiwi

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 avatar Dec 04 '19 18:12 Coding-Kiwi

References https://github.com/sindresorhus/electron-store/issues/88

But mainly: https://github.com/sindresorhus/conf/issues/85

Coding-Kiwi avatar Dec 04 '19 18:12 Coding-Kiwi

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.

leasontou avatar Dec 05 '19 07:12 leasontou

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.

sindresorhus avatar Feb 22 '20 09:02 sindresorhus

same bug guys

fedyfausto avatar May 11 '20 15:05 fedyfausto

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.

EngJay avatar May 27 '20 16:05 EngJay