store: Breaking change in storage plugin in v3.3.0

I’m submitting a…


[x] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => https://github.com/ngxs/store/blob/master/CONTRIBUTING.md
[ ] Other... Please describe:

Current behavior

The fix for issue #538 changed line 26 of storage.plugin.ts to:

if (typeof val !== 'undefined' && val !== null) {

However, on line 61, getValue for a key that does not yet exist will return undefined. In turn, setItem on line 65 will serialize that as the string'undefined':

if (key !== '@@STATE') {
  val = getValue(nextState, key);
}

try {
  this._engine.setItem(key, options.serialize(val));

The effect is that the test on line 26 passes, the deserialization of the string 'undefined' fails and the value is set to an empty object.

Expected behavior

Instead, the value should ultimately be set to its default state, as it was in v3.2.0.

I think the test on line 26 could be coded as:

if (val !== 'undefined' && val != null) {

I have patched it to be so in node_modules/@ngxs/storage-plugin/fesm5/ngxs-storage-plugin.js and everything behaves as expected.

Note that val != null is equivalent to val !== undefined && val !== null as per https://stackoverflow.com/questions/38648087/checking-for-null-or-undefined.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (16 by maintainers)

Most upvoted comments

Yes – works perfectly! Many thanks @markwhitfeld and @MrCroft, @eranshmil and @splincode for helping out so quickly.

The reason for that PR was to fix custom engine under NativeScript. If you revert that, you will break for other users. It should be thoroughly investigated.