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

Arrays indexing fails with numeric keys #571

Closed
xrchz opened this issue Mar 15, 2023 · 3 comments
Closed

Arrays indexing fails with numeric keys #571

xrchz opened this issue Mar 15, 2023 · 3 comments
Labels

Comments

@xrchz
Copy link

xrchz commented Mar 15, 2023

There seem to be various problems with array processing:

> const db = new JsonDB(new Config('foo'))
undefined
> await db.push('/foo/1[]', 'hello')
undefined
> await db.push('/foo/1[]', 'world')
undefined
> await db.count('/foo/1')
2
> await db.getData('/foo/1[-1]')
'd'  // expected: 'world'
> await db.getData('/foo/1[1]')
'o' // expected: 'world'
> await db.getData('/foo/1[0]')
'w' // expected: 'hello'
> await db.getData('/foo/1')
[ 'hello', 'world' ]
> await db.push('/foo/2[]', {test: 1, baz: 2})
undefined
> await db.push('/foo/2[]', {test: 3, baz: 4})
undefined
> await db.count('/foo/2')
2
> await db.getData('/foo/2[-1]')
{ test: 3, baz: 4 }
> await db.getData('/foo/2[-2]')
Uncaught DataError: DataPath: /foo/2[-2]. . Can't find index -2 in array 2
    at recursiveProcessDataPath (node_modules/node-json-db/dist/JsonDB.js:88:27)
    at recursiveProcessDataPath (node_modules/node-json-db/dist/JsonDB.js:98:20)
    at JsonDB.retrieveData (node_modules/node-json-db/dist/JsonDB.js:103:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  inner: undefined,
  id: 10
} // expected: {test: 1, baz: 2}
@Belphemur
Copy link
Owner

Could you attach the resulting JSON file ?

This is indeed a curious behavior.

@xrchz
Copy link
Author

xrchz commented Mar 16, 2023

It's as expected:

{"foo":{"1":["hello","world"],"2":[{"test":1,"baz":2},{"test":3,"baz":4}]}}

github-actions bot pushed a commit that referenced this issue Apr 9, 2023
# [2.2.0](v2.1.5...v2.2.0) (2023-04-09)

### Bug Fixes

* **Array:** Fix issue with array that have numerical property key ([db7b5cb](db7b5cb)), closes [#571](#571)

### Features

* **getObjectDefault:** Add method `getObjectDefault` to get an object or a default value when the path is leading to non existing data. ([d47786b](d47786b)), closes [#582](#582)
@github-actions
Copy link
Contributor

github-actions bot commented Apr 9, 2023

🎉 This issue has been resolved in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants