Skip to content

How to increment a nested object field ? #1299

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

Closed
5 of 6 tasks
sadortun opened this issue Feb 17, 2021 · 2 comments · Fixed by #1301
Closed
5 of 6 tasks

How to increment a nested object field ? #1299

sadortun opened this issue Feb 17, 2021 · 2 comments · Fixed by #1301
Assignees
Labels
type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@sadortun
Copy link
Contributor

sadortun commented Feb 17, 2021

New Issue Checklist

Issue Description

This is a simpler case version of issue #6687. I'm not sure if i'm doing anything wrong, but i think this should work

I'm trying to increment a value of a newly created object.

Ex:

db.test.findOneAndUpdate(
    {"_id":"20mMYzLTjx"} ,
    {$inc:{ "a.b.c.d" : 1 }}
)

Steps to reproduce

const obj= await Parse.Object
    .extend('test')
    .createWithoutData('20mMYzLTjx').fetch()

// obj.get('a') === {}   (my schema defaults a to {}

obj.increment('a.b.c.d' , 1 )
await obj.save()

Actual Outcome

TypeError: Cannot read property 'd' of undefined at Object.estimateAttributes (.....parse-server\node_modules\parse\lib\node\ObjectStateMutations.js:234:28)

Seems to me that https://github.com/parse-community/Parse-SDK-JS/blob/master/src/ObjectStateMutations.js#L140

          for (let i = 0; i < fields.length - 1; i++) {
-            object = object[fields[i]];
+            object = object[fields[i]] || {}; 
          }

Expected Outcome

Path a.b.c.d get created and persisted

Failing Test Case / Pull Request

  • 🤩 I submitted a PR with a fix and a test case.
  • 🧐 I submitted a PR with a failing test case.

Environment

Server

  • Parse Server version: v4.5.0 / 3d76643286d41b954b6c8e2d6614657c891a31e7
  • Operating system: Win 10
  • Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): local

Database

  • System (MongoDB or Postgres): Mongo
  • Database version: 4.4.4
  • Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): remote/self hosted

Client

  • SDK (iOS, Android, JavaScript, PHP, Unity, etc): Js
  • SDK version: 2.17.0

Logs

@dplewis
Copy link
Member

dplewis commented Feb 17, 2021

Nice catch! We will look into this shortly.

@dplewis dplewis self-assigned this Feb 17, 2021
@dplewis dplewis added the type:bug Impaired feature or lacking behavior that is likely assumed label Feb 17, 2021
@dplewis
Copy link
Member

dplewis commented Feb 19, 2021

@sadortun A little back history. Nested objects were originally supposed to be for simple objects as complex objects could be broken up into different fields and classes.

If you have time check out this thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants