deepstream.io icon indicating copy to clipboard operation
deepstream.io copied to clipboard

Setting An element to an array with index = 0 creates an Object

Open redlock opened this issue 9 years ago • 9 comments

Hi, While I was trying to set a record array with index 0 like so

 record.set(`items[0]`, 51)

deepstream create an object rather than an array as follows: { items: { "0" : 51 } }

I traced the error to json-path.js line 40:

} else if (this._tokens[i + 1] && !isNaN(this._tokens[i + 1])) {

in this conditional that checks for an index, if the value of the index in this._tokens[i + 1] is zero it will fail.

A quick fix is to replace the conditional with this:

if (this._tokens[i + 1] !== undefined && !isNaN(this._tokens[i + 1])) {

Hopefully you can patch it up soon. And thanks for your great work with deepstream.

redlock avatar Apr 12 '17 09:04 redlock

Using arrays with systems like deepstream can get messy. Because clients do not have identical states at any given time, making changes to array elements can cause merge issues when two clients update the same array at the same time in different ways.

Lets say for example client a removes element 5, client b updates element 5. If client b gets processed first, the the update will be removed by client a. If client a wins out, client b will actually update was was element 6.

You can only reliably use arrays if you update the entire array in a single operation.

If you want to add remove element, you are best using an identifier that is sequential based on time, but gives each element a unique id that will not be confused in a merge operation.

The client does have a helper method, getUid(), that you can use to generate the identifier.

datasage avatar Apr 12 '17 20:04 datasage

That is true for your scenario. My use case has one publisher that modifies that array and the rest of the clients are subscribers with no ability to modify. Nonetheless, the issue I mention above is still a bug in the json-path.js file as it doesn't behave as intended.

redlock avatar Apr 12 '17 20:04 redlock

hey @redlock, we will be looking to fix this once easter is over. Thanks for raising!

yasserf avatar Apr 16 '17 10:04 yasserf

@WolframHempel @yasserf @AlexBHarley FYI the website link for the Contributor License Agreement is broken, so I couldn't do a PR but below is my deepstream.io/master patch with my FIXes for the server-side issues #651 and #646.

The two file updates are .txt attachments on #651 (json-path.js/txt and json-pathSpec.js/txt deepstream.io/master 2.2.0)

honorcode avatar Apr 18 '17 21:04 honorcode

FYI the website link for the Contributor License Agreement is broken, so I couldn't do a PR but ^^^ is my deepstream.io/master patches with my FIXes for the server-side issues #651 and #646.

honorcode avatar Apr 18 '17 21:04 honorcode

@WolframHempel @yasserf @AlexBHarley Opened related java-client issue found when testing: https://github.com/deepstreamIO/deepstream.io-client-java/issues/84

honorcode avatar Apr 18 '17 22:04 honorcode

when tested as mentioned in the first comment, the record is being set as an array, not an object.

result of testing: Screen Shot 2017-08-03 at 12.42.12.png

mrby avatar Aug 03 '17 10:08 mrby

Can we also try this out with the java sdk? Looks like its fixed though!

yasserf avatar Aug 03 '17 10:08 yasserf

Closing in favour of https://github.com/deepstreamIO/deepstream.io/issues/1046

yasserf avatar May 03 '20 07:05 yasserf