andreyvit / json-diff

Structural diff for JSON files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ES6 diffString Cannot read properties of null (reading 'key')

justinhessdev opened this issue · comments

diffString is erroring out when it comes to an empty array of objects:

  const patch = diffString(
    { 
      services: [],
    },
    {
      services: [{ service_code: '123' }],
    }
  );
index.js:54 TypeError: Cannot read properties of null (reading 'key')
    at JsonDiff.scalarize (index.js:103)
    at JsonDiff.arrayDiff (index.js:144)
    at JsonDiff.diff (index.js:289)
    at JsonDiff.objectDiff (index.js:39)
    at JsonDiff.diff (index.js:286)
    at diff (index.js:315)
    at diffString (index.js:319)
    at DiffTest (index.stories.js:54)
    at finalStoryFn (story_store.js:568)
    at hooks.js:182

I need to compare many large objects that I shouldn't mutate. Some will have empty arrays of objects which will cause the app to crash.

Would it be possible to fix this?

** Update: this was not working on version 0.6.1
apparently there was a fix earlier today -- working on 0.6.3

commented

This is happening to me in version 0.7.3, is there a fix for this?

@ivan-015 I cannot repro

$ cd bin
$ node
Welcome to Node.js v16.13.0.
Type ".help" for more information.
> require('../lib/index').diffString(    { 
...       services: [],
...     },
...     {
.....       services: [{ service_code: '123' }],
.....     }
...   );
' {\n' +
  '   services: [\n' +
  '\x1B[32m+    {\x1B[39m\n' +
  '\x1B[32m+      service_code: "123"\x1B[39m\n' +
  '\x1B[32m+    }\x1B[39m\n' +
  '   ]\n' +
  ' }\n'
commented

@ewoudenberg thanks for your prompt reply. This was my mistake. I had this package installed in a Docker container and it was caching the package's old version. After running docker with no cache, the error went away. Thanks again!