The Wayback Machine - https://web.archive.org/web/20200908040559/https://github.com/parse-community/parse-server/issues/6687
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

How to increment array nested object field? #6687

Open
zmras opened this issue May 15, 2020 · 5 comments
Open

How to increment array nested object field? #6687

zmras opened this issue May 15, 2020 · 5 comments

Comments

@zmras
Copy link

@zmras zmras commented May 15, 2020

Hi! Let's assume I have the following data:

obj = {
  _id: 'someId',
  items: [
    { value: 'a', count: 5 },
    { value: 'b', count: 1 },
  ]
}

Please, I want to know how can I use obj.increment() to increment the count field of the first element in items?

I have tried obj.increment('items.0.count') but it doesn't work. I got the following error: Cannot read property '0' of undefined

@zmras zmras closed this May 22, 2020
@mtrezza
Copy link
Contributor

@mtrezza mtrezza commented May 22, 2020

To clarify my previous comment: what you want to do is not documented and not supported in Parse Server, but I think you're onto something. It makes sense to support it since it is supported by MongoDB.

I had a look into why this doesn't work and I found several roadblocks:

1) Parse JS SDK:

The SDK assumes that a nested key path does not require to traverse over an array element. It fails to traverse the path, because 0 is not an object key but an array index in the example above.

https://github.com/parse-community/Parse-SDK-JS/blob/e5094aeb5c5d8b2a396f2171f9cb26a70f94a64b/src/ObjectStateMutations.js#L126-L137

This causes the error: TypeError: Cannot read property '0' of undefined.
Workaround is to fetch the object before incrementing.

2) Parse JS SDK:

For nested keys the SDK validates the existence of said key, otherwise it does not set the increment operator on that key. Effectively, this does not allow to increment a nested key for an object that has been instantiated with Parse.Object.createWithoutData(). I'm not sure why that is only checked for nested keys and what implications it has to change that.

https://github.com/parse-community/Parse-SDK-JS/blob/e5094aeb5c5d8b2a396f2171f9cb26a70f94a64b/src/ParseObject.js#L696-L703

This causes the increment instruction to be ignored without throwing an error.
Workaround is to fetch the object before incrementing.
Same root cause as for issue b) in #6455 (comment).

3) Parse Server:

SchemaController expects the Parse.Object field type to be of Object when the key to modify contains a dot:

if (fieldName.indexOf('.') > 0) {
// subdocument key (x.y) => ok if x is of type 'object'
fieldName = fieldName.split('.')[0];
type = 'Object';
}

This fails the schema validation because the field in the example above is of type Array which causes a type mismatch with the expected type Object.

This causes the error:
ParseError: 111 schema mismatch for MyClass.myField; expected Array but got Object


Conclusion:

  • I would consider roadblocks 1 and 2 to be a bug, regardless of the current issue.
  • Roadblock 3 is not a bug but needs to be changed in order to implement the feature of incrementing (or even setting?) values of nested key paths that include arrays.
@zmras
Copy link
Author

@zmras zmras commented May 23, 2020

I really thank you for the explanation. I think I will use mongodb collection.updateOne() with $inc operator directly to increment the field until the feature is considered to be implemented.

@mtrezza
Copy link
Contributor

@mtrezza mtrezza commented May 23, 2020

Do you want to reopen this issue to keep track of it?

Sent with GitHawk

@zmras
Copy link
Author

@zmras zmras commented Jun 2, 2020

Ok! I'll reopen it

@ruhci28
Copy link

@ruhci28 ruhci28 commented Jul 16, 2020

hye, is this issue open .?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.