Skip to content

How to increment a nested object field ? #1299

Closed
@sadortun

Description

@sadortun

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

Metadata

Metadata

Assignees

Labels

type:bugImpaired feature or lacking behavior that is likely assumed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions